Sparse Vector Dot Product
Updated
Problem statement
Given two equal-length arrays first and second, treat each as a vector of signed integers. Conceptually construct a sparse representation that stores only nonzero index-value pairs, then return their dot product as a signed 64-bit integer.
The dot product is the sum of first[i] * second[i] over all indices. Zero entries do not need explicit storage.
Function
sparseDotProduct(first: int[], second: int[]) → longExamples
Example 1
first = [1,0,0,2,3]second = [0,3,0,4,0]return = 8Only index 3 is nonzero in both vectors, contributing 2 * 4 = 8.
Example 2
first = [0,-2,0,5]second = [7,3,0,-1]return = -11The shared nonzero indices contribute -2 * 3 + 5 * -1 = -11.
Constraints
0 <= first.length == second.length <= 200000.-10^6 <= first[i], second[i] <= 10^6.- The final dot product fits a signed 64-bit integer.
More Anduril interview questions
- Pacific Atlantic Water FlowPHONE SCREEN · Seen Aug 2026
- Choose the Front Football TeamPHONE SCREEN · Seen May 2026
- Maximum Visible DronesPHONE SCREEN · Seen Jun 2024
- Build a Sorted Circular Singly Linked ListPHONE SCREEN · Seen Feb 2024
- Add Word and Suffix SearchONSITE INTERVIEW · Seen Aug 2026
- Binary Tree Preorder TraversalONSITE INTERVIEW · Seen Aug 2026
- Operator-First Array CalculatorONSITE INTERVIEW · Seen Aug 2026
- Daily TemperaturesONSITE INTERVIEW · Seen Jun 2024
- Highlighted Context WindowsONSITE INTERVIEW · Seen Jun 2024
Similar questions at other companies
- Insert Delete GetRandom O(1)Charta Health · PHONE SCREENCharta Health interview questions
- Portfolio Tickers with Constant-Time Random PickCitadel · PHONE SCREENCitadel interview questions
- Repeated Shortest Word Distance QueriesLinkedIn · PHONE SCREENLinkedIn interview questions
- Build a Pivot Table with TotalsSigma Computing · PHONE SCREEN
- Process On-Call Routing OperationsOpenAI · PHONE SCREENOpenAI interview questions
- Stateful Paginated Fetch NLyft · PHONE SCREENLyft interview questions
- Linear-Probing Key-Value StoreEase · PHONE SCREEN
- Escape Room LeaderboardPinterest · PHONE SCREEN · ONSITE INTERVIEWPinterest interview questions
- Role-Based Network Access ControlOkta · PHONE SCREENOkta interview questions
- Track Package Receipt GapsAmazon · PHONE SCREEN