Watch 10 video solutions for Dot Product of Two Sparse Vectors, a medium level problem involving Array, Hash Table, Two Pointers. This walkthrough by Cracking FAANG has 16,773 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given two sparse vectors, compute their dot product.
Implement class SparseVector:
SparseVector(nums) Initializes the object with the vector numsdotProduct(vec) Compute the dot product between the instance of SparseVector and vecA sparse vector is a vector that has mostly zero values, you should store the sparse vector efficiently and compute the dot product between two SparseVector.
Follow up: What if only one of the vectors is sparse?
Example 1:
Input: nums1 = [1,0,0,2,3], nums2 = [0,3,0,4,0] Output: 8 Explanation: v1 = SparseVector(nums1) , v2 = SparseVector(nums2) v1.dotProduct(v2) = 1*0 + 0*3 + 0*0 + 2*4 + 3*0 = 8
Example 2:
Input: nums1 = [0,1,0,0,0], nums2 = [0,0,0,0,2] Output: 0 Explanation: v1 = SparseVector(nums1) , v2 = SparseVector(nums2) v1.dotProduct(v2) = 0*0 + 1*0 + 0*0 + 0*0 + 0*2 = 0
Example 3:
Input: nums1 = [0,1,0,0,2,0,0], nums2 = [1,0,0,0,3,0,4] Output: 6
Constraints:
n == nums1.length == nums2.length1 <= n <= 10^50 <= nums1[i], nums2[i] <= 100Problem Overview: You receive two vectors that mostly contain zeros. Instead of storing every element, you design a sparse vector structure and implement a function that returns the dot product between two such vectors.
Approach 1: Direct Array Iteration (O(n) time, O(1) space)
The straightforward solution iterates through both vectors and multiplies values at the same index. Maintain a running sum: result += nums1[i] * nums2[i]. This works because the dot product is defined as the sum of element‑wise products. The drawback is wasted work when most values are zero. You still process every index even if the product contributes nothing. This approach is acceptable when vectors are dense or when the input is already stored as full arrays.
Approach 2: Hash Map Sparse Representation (O(k1 + k2) time, O(k1 + k2) space)
A sparse vector only needs to store non‑zero values. During initialization, iterate through the array and insert (index → value) pairs into a hash table whenever the value is not zero. If the vector contains k non‑zero elements, the storage shrinks from O(n) to O(k).
To compute the dot product, iterate through the smaller map and check whether the same index exists in the other map. Each lookup is O(1) on average. If both vectors contain a non‑zero value at index i, multiply them and add to the result. Zero entries never appear in the map, so they are skipped automatically.
This design drastically reduces work when the vectors are sparse. Instead of scanning the entire array length n, you only process indices that matter. The runtime becomes proportional to the number of stored elements rather than the vector size.
Some implementations also keep non‑zero entries sorted and use a two pointers technique to traverse both lists simultaneously. Both methods rely on the same core idea: ignore zero values and operate only on meaningful entries.
Recommended for interviews: The sparse hash map design is what interviewers expect. Start by explaining the naive array scan to show understanding of the dot product definition. Then optimize by storing only non‑zero values. This demonstrates awareness of sparse data structures, practical memory optimization, and efficient lookup using a hash table.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Direct Array Iteration | O(n) | O(1) | Vectors are dense or already stored as full arrays |
| Hash Map Sparse Representation | O(k1 + k2) | O(k1 + k2) | Vectors contain many zeros; only non‑zero elements are stored |
| Sorted Non‑Zero Lists + Two Pointers | O(k1 + k2) | O(k1 + k2) | Non‑zero entries are stored as sorted pairs and traversed simultaneously |