Sparse Vector Dot Product
Updated
Problem statement
You are given two integer arrays nums1 and nums2 of the same length n. Each array is a vector, and many of its entries may be 0.
Return their dot product: the sum of nums1[i] * nums2[i] over every index i from 0 to n - 1.
Function
dotProduct(nums1: int[], nums2: int[]) → longExamples
Example 1
nums1 = [0,3,0,0,2]nums2 = [5,4,0,1,6]return = 24Only indexes 1 and 4 are nonzero in both arrays. The sum is 3 * 4 + 2 * 6 = 24. Index 0 and index 3 add nothing, because one side is 0.
Example 2
nums1 = [7,0,0]nums2 = [0,0,9]return = 0The two arrays have no nonzero entry at the same index, so every product is 0.
Example 3
nums1 = [100000,-100000,100000]nums2 = [100000,-100000,100000]return = 30000000000Each product is 10^10, and the sum is 3 * 10^10. This is larger than a 32-bit integer can hold.
Example 4
nums1 = [0,-4,0,5]nums2 = [0,3,0,2]return = -2The products at indexes 1 and 3 are -12 and 10, so the sum is -2.
Constraints
1 <= n <= 10^5nums1.length == nums2.length == n-10^5 <= nums1[i], nums2[i] <= 10^5- Each array has at most
1000nonzero entries.
More WhatNot Phone Screen questions
- Adjacent Pair and Run RemovalPHONE SCREEN · Seen Nov 2026
- Merge Sorted Squares of Two ArraysPHONE SCREEN · Seen Nov 2026
- User Journey PathsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Knight Dialer SequencesPHONE SCREEN · Seen Oct 2026
- Reachable Exits in an Obstacle GridPHONE SCREEN · Seen Oct 2026
- Palindrome with One DeletionPHONE SCREEN · Seen Oct 2026
- Search a 2D MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Attraction Route Without Reusing TrailsPHONE SCREEN · Seen Sep 2026
- Earliest Car for Each PassengerPHONE SCREEN · Seen Sep 2026
- Minimum Stores for a Shopping ListPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Unique Pairs in a 2D Matrix Summing to TargetAmazon · PHONE SCREEN
- Repeated Shortest Word Distance QueriesLinkedIn · PHONE SCREENLinkedIn interview questions
- Disjoint Target-Sum TripletsAmerican Express · PHONE SCREENAmerican Express interview questions
- Sparse Vector Dot ProductAnduril · PHONE SCREENAnduril interview questions
- Sparse Vector Dot Product from TuplesMeta · ONSITE INTERVIEWMeta interview questions
- Count Cross-Array Target-Sum PairsVisa · OAVisa interview questions
- Two SumMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Shortest Window with Required CountsOracle · ONSITE INTERVIEWOracle interview questions
- Count Belt Sections With K Fruit Pairs (Forward Deployed Engineer)Boston Consulting Group · OABoston Consulting Group interview questions
- Find Two-Sum or Three-Sum Index CombinationsGoogle · ONSITE INTERVIEWGoogle interview questions