Dot Product of Two Sparse Embedding Vectors

Last updated: November 29, 2026

Quick Overview

Design a SparseVector class built from a dense list of numbers that stores only nonzero entries, and implement dot(other) that returns the dot product with another SparseVector of the same length.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Design a SparseVector class built from a dense list of numbers that stores only nonzero entries, and implement dot(other) that returns the dot product with another SparseVector of the same length.

Sparse features are everywhere in Meta's ranking and ads models, and this question checks representation choice as much as code. Interviewers push on hash map versus sorted index-value pairs, and what to do when one vector is far denser than the other.

What the Interviewer Expects
  • Stores only nonzero entries with their indices
  • Iterates over the smaller vector for the dot product
  • Compares hash-map and sorted-pairs representations
  • Mentions binary search when sizes differ greatly
  • Analyzes complexity in terms of nonzero counts
Key Topics to Cover
Hash map
Two pointers
Sparse data
Class design
Vectors
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Only one vector is sparse; what changes?
  • Hash maps are banned because of memory overhead; what do you use?
  • How would you compute dot products of one query vector against millions of stored vectors?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Vectors are the same length n, possibly 10^5 or more, with few nonzeros. Construction happens once; dot may be called many times. Return a number....

High-Level Design

Store a sorted list of (index, value) pairs, since the constructor scans indices in order for free. For the dot product, run a two-pointer merge on the index lists: equal indices contribute `value_a...


Submit Your Answer
Markdown supported