Medium · System Design

Dot Product of Two Sparse Vectors

Given two integer arrays nums1 and nums2 of the same length, build a SparseVector from each that stores the vector compactly, and return the dot product of the two vectors, the sum of nums1[i] · nums2[i] over all i.

Examples

Example 1

[1,0,0,2,3] · [0,3,0,4,0]

Output: 8

Example 2

[2,3,0,5,0] · [0,4,0,6,7]

Output: 42

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More System Design problems