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.
Implement a SparseVector class for high-dimensional vectors where most entries are zero.
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]
.
Add a method (or standalone function) to compute the dot product between two sparse vectors:
dot(other: SparseVector) -> float
self.dim != other.dim
, return an error (e.g., raise an exception or return a special error value as specified by the interviewer).
dim
entries).
Extend the same class with a method:
cosine_similarity(other: SparseVector) -> float