PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|Home/Coding & Algorithms/Apple

Implement sparse vector dot product and cosine similarity

Last updated: Mar 29, 2026

Quick Overview

This question evaluates implementation of sparse data structures and efficient algorithms for high-dimensional vector operations, specifically the dot product and cosine similarity, testing knowledge of sparsity-aware computation and linear algebra concepts such as vector norms.

  • Medium
  • Apple
  • Coding & Algorithms
  • Data Scientist

Implement sparse vector dot product and cosine similarity

Company: Apple

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

## Sparse Vector Class Implement a `SparseVector` class for high-dimensional vectors where most entries are zero. ### Representation Assume each vector has: - `dim: int` (the full dimensionality, e.g., 1,000,000) - `values: Map[int, float]` storing only non-zero entries (index → value). Indices are 0-based and must be in `[0, dim-1]`. ### Task 1 — Sparse vector multiplication (dot product) Add a method (or standalone function) to compute the dot product between two sparse vectors: - `dot(other: SparseVector) -> float` - If `self.dim != other.dim`, return an error (e.g., raise an exception or return a special error value as specified by the interviewer). - Must be efficient for sparse data (avoid iterating over all `dim` entries). ### Task 2 — Cosine similarity Extend the same class with a method: - `cosine_similarity(other: SparseVector) -> float` - If dimensions don’t match, return an error. - Define cosine similarity as: \[ \cos(\theta)=\frac{\mathbf{x}\cdot \mathbf{y}}{\|\mathbf{x}\|\,\|\mathbf{y}\|} \] - Specify what you do if either vector has zero norm (e.g., return 0, or return an error). ### Output/Expectations - Provide clean, readable code (any mainstream language is acceptable). - Briefly state time complexity in terms of number of non-zeros (nnz).

Quick Answer: This question evaluates implementation of sparse data structures and efficient algorithms for high-dimensional vector operations, specifically the dot product and cosine similarity, testing knowledge of sparsity-aware computation and linear algebra concepts such as vector norms.

Related Interview Questions

  • Compute Earliest Bus Arrival - Apple (medium)
  • Find the Extra Edge - Apple (hard)
  • Rotate a Matrix In Place - Apple (medium)
  • Encode and Rebuild a Binary Tree - Apple (hard)
  • Wrap Matching Substrings in Bold Tags - Apple (medium)
Apple logo
Apple
Jul 15, 2025, 12:00 AM
Data Scientist
Technical Screen
Coding & Algorithms
2
0

Sparse Vector Class

Implement a SparseVector class for high-dimensional vectors where most entries are zero.

Representation

Assume each vector has:

  • dim: int (the full dimensionality, e.g., 1,000,000)
  • values: Map[int, float] storing only non-zero entries (index → value). Indices are 0-based and must be in [0, dim-1] .

Task 1 — Sparse vector multiplication (dot product)

Add a method (or standalone function) to compute the dot product between two sparse vectors:

  • dot(other: SparseVector) -> float
  • If self.dim != other.dim , return an error (e.g., raise an exception or return a special error value as specified by the interviewer).
  • Must be efficient for sparse data (avoid iterating over all dim entries).

Task 2 — Cosine similarity

Extend the same class with a method:

  • cosine_similarity(other: SparseVector) -> float
  • If dimensions don’t match, return an error.
  • Define cosine similarity as:

cos⁡(θ)=x⋅y∥x∥ ∥y∥\cos(\theta)=\frac{\mathbf{x}\cdot \mathbf{y}}{\|\mathbf{x}\|\,\|\mathbf{y}\|}cos(θ)=∥x∥∥y∥x⋅y​

  • Specify what you do if either vector has zero norm (e.g., return 0, or return an error).

Output/Expectations

  • Provide clean, readable code (any mainstream language is acceptable).
  • Briefly state time complexity in terms of number of non-zeros (nnz).

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Apple•More Data Scientist•Apple Data Scientist•Apple Coding & Algorithms•Data Scientist Coding & Algorithms
PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.