← Meta Interview Insights

Meta·Software Engineer·Technical Phone Screen·Senior

Senior
May 2026

Summary

Meta Research Engineer coding screen, one question on sparse vectors with a follow-up that actually made me think harder than the main problem.

Questions Asked (1)

Q1

Given two sparse vectors, design a class that stores them efficiently and computes their dot product. Follow-up: how does your approach change if only one of the two vectors is sparse?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The main part is pretty approachable if you know to use a hashmap to store only the nonzero indices.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the definition of sparsity and the expected operations (e.g., dot product frequency, vector size). Propose a hash map or list of (index, value) pairs to store only non-zero elements, then compute the dot product by iterating over the smaller structure and looking up indices in the other. For the follow-up, discuss how the choice of data structure and algorithm changes when only one vector is sparse, focusing on time and space trade-offs.

Pro tip: Mention that for the sparse-sparse case, you can iterate over the smaller vector's non-zero entries and use binary search if the other is sorted, achieving O(min(nnz1, nnz2) log(max(nnz1, nnz2))) time; for sparse-dense, direct iteration over the sparse vector's entries with O(1) array access is optimal. This shows you consider practical performance beyond naive approaches.

1. Clarify requirements and constraints

Ask about the expected size of vectors, definition of sparsity, frequency of dot product calls, and whether vectors are mutable. This ensures you design the right trade-offs.

2. Choose data structures for sparse vectors

Propose storing only non-zero elements using a hash map (index -> value) or a sorted list of (index, value) pairs. Discuss pros and cons: hash map gives O(1) average lookup but no order; sorted list allows binary search and efficient iteration.

3. Implement dot product for sparse-sparse

Iterate over the non-zero entries of the smaller vector, and for each index, check if it exists in the other vector's structure. If using hash map, lookup is O(1); if using sorted lists, use two-pointer or binary search. Sum the products of matching indices.

4. Address follow-up: one vector sparse, one dense

If one vector is dense, store it as a regular array. For dot product, iterate over the sparse vector's non-zero entries and directly access the dense array at those indices, multiplying and summing. This is O(nnz) time, which is optimal.

5. Analyze time and space complexity

Compare approaches: sparse-sparse with hash maps O(nnz1 + nnz2) time, O(nnz1 + nnz2) space; with sorted lists O(min(nnz1, nnz2) log(max(nnz1, nnz2))) time. Sparse-dense: O(nnz) time, O(n) space for dense. Discuss trade-offs and when to choose each.

Key Points to Mention

  • Definition of sparsity: typically when number of non-zero elements (nnz) is much less than total size.
  • Data structure choices: hash map vs. sorted array/list, and their impact on lookup and iteration.
  • Time complexity for dot product: O(nnz1 + nnz2) with hash maps, or O(min(nnz1, nnz2) log(max(nnz1, nnz2))) with binary search on sorted lists.
  • Space complexity: O(nnz) for sparse storage, O(n) for dense storage.
  • Follow-up: when one vector is dense, use direct array access for O(nnz) time, which is optimal.
  • Trade-offs: hash map offers faster average lookup but higher memory overhead; sorted list allows binary search and cache-friendly iteration but requires sorting.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.