Sparse Vector Dot Product from Tuples
Updated
Problem statement
Two equal-length integer vectors are stored only by their nonzero entries. Each row [index, value] in first or second represents one nonzero value at that zero-based index.
Both tuple arrays use strictly increasing, unique, in-range indices. Compute and return the vectors' dot product as a signed 64-bit integer. Indices missing from a tuple array have value zero.
The vector length length establishes the shared dimension and may be zero.
Function
sparseDotProduct(length: int, first: int[][], second: int[][]) → longExamples
Example 1
length = 5first = [[0,1],[3,2],[4,3]]second = [[1,3],[3,4]]return = 8Only index 3 appears in both vectors, contributing 2 * 4 = 8.
Example 2
length = 4first = [[1,-2],[3,5]]second = [[0,7],[1,3],[3,-1]]return = -11The shared indices contribute -2 * 3 + 5 * -1 = -11.
Example 3
length = 6first = []second = [[2,9]]return = 0The first vector is all zero, so the dot product is zero.
Constraints
0 <= length <= 10^9.0 <= first.length, second.length <= 200000.- Every tuple contains exactly
[index, value]. - Within each operand, indices are unique, strictly increasing, and satisfy
0 <= index < length. - Every stored value is nonzero and lies between
-10^9and10^9, inclusive. - The final dot product fits a signed 64-bit integer.
More Meta Onsite Interview questions
- Multi-Head Attention Forward PassPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Run-Length Encoded Vector Dot ProductPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Mutual-Friend RecommendationsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Deterministic K-Means AssignmentsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Linear Layer Forward and Backward PassesONSITE INTERVIEW · Seen Sep 2026
- Reservoir Sampling with Recorded DrawsONSITE INTERVIEW · Seen Sep 2026
- Stochastic Gradient Descent with MomentumONSITE INTERVIEW · Seen Sep 2026
- Top K Largest Values with a HeapONSITE INTERVIEW · Seen Sep 2026
- Partition a Card Deck into FifteensONSITE INTERVIEW · Seen Sep 2026
- Insert into a Sorted Circular ListONSITE INTERVIEW · Seen Aug 2026
Similar questions at other companies
- Two SumMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Shortest Window with Required CountsOracle · ONSITE INTERVIEWOracle interview questions
- Find Two-Sum or Three-Sum Index CombinationsGoogle · ONSITE INTERVIEWGoogle interview questions
- Unique Pairs in a 2D Matrix Summing to TargetAmazon · PHONE SCREEN
- Count Cross-Array Target-Sum PairsVisa · OAVisa interview questions
- Sparse Vector Dot ProductWhatNot · PHONE SCREENWhatNot interview questions
- Count Belt Sections With K Fruit Pairs (Forward Deployed Engineer)Boston Consulting Group · OABoston Consulting Group interview questions
- Repeated Shortest Word Distance QueriesLinkedIn · PHONE SCREENLinkedIn interview questions
- Disjoint Target-Sum TripletsAmerican Express · PHONE SCREENAmerican Express interview questions
- Efficient TeamsExpedia · OAExpedia interview questions