Dynamic Pair Sum Queries
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
ZipRecruiter reported this one in September 2024, and the detail that matters is in the statement: array b is never modified. That's the whole game. You get arrays a and b, then a mixed stream of point updates on a and pair-count queries for a target sum x. If you're taking this OA in the next couple of days, it's a hash-table counting problem dressed up as a query problem. Brute force dies fast. Keep it in your head as frequency maps, not nested loops. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but the idea here is small enough to hold yourself.
The problem
For this exercise, the mutable source array is exposed as the first parameter a; this is only a callable-adapter naming choice. You are given two integer arrays a and b and a sequence of queries. Process the queries in order. A query [0, i, x] adds x to a[i]. A query [1, x] asks for the number of index pairs (i, j) such that the current values satisfy a[i] + b[j] = x. Return the answers to all type-1 queries in their original order. Function processPairSumQueries(a: int[], b: int[], queries: int[][]) → long[] Examples Example 1 a = [1,4] b = [1,2,3] queries = [[1,5],[0,0,2],[1,5]] return = [1,2] Before the update, only a[1] + b[0] = 4 + 1 = 5, so the first answer is 1. The update changes a[0] from 1 to 3. Then both 3 + 2 and 4 + 1 equal 5, so the second answer is 2. Constraints a, b, and queries are non-empty. Every type-0 query has the form [0, i, x] with 0 <= i < a.length. Every type-1 query has the form [1, x]. Array b is never modified.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Build a frequency map of b once, since b never changes. Each query of type 1 then needs the count of pairs where a[i] + b[j] = x. For every a[i], add freqB[x - a[i]]. Done naively that's O(n) per query, which can be too slow with many queries. The better move is to keep a frequency map of a as well, and maintain it on updates: decrement the old value of a[i], increment the new one. Then a query iterates over distinct values in a's map and sums countA[v] * countB[x - v]. Pitfalls: the answer needs a long, so multiply in 64-bit. Update the actual array a too, or repeated updates to the same index will use stale values. Also remember x in an update is added, not assigned. If you freeze live, StealthCoder can surface the map-maintenance pattern for you in the OA.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Dynamic Pair Sum Queries cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass ZipRecruiter's OA.
ZipRecruiter reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Dynamic Pair Sum Queries FAQ
What's the trick in Dynamic Pair Sum Queries?+
Count frequencies instead of looping over pairs. Build a hash map of b once because it never changes. For a query, sum countA[v] times countB[x - v] over values in a. Updates just move one count from the old value to the new value.
How hard is this ZipRecruiter OA question really?+
Medium at most. The logic is short, but the traps are real: 64-bit results, updating a[i] correctly, and picking a query strategy that doesn't time out. If you know hash maps, you can finish it quickly.
Why must the answer be a long?+
The pair count can reach roughly len(a) times len(b), which overflows a 32-bit int on big inputs. Use a 64-bit type for the running sum and for each product of counts, so intermediate multiplications don't wrap.
How do I handle the update query [0, i, x]?+
Read the current a[i], decrement its count in the frequency map of a, and remove the key if it hits zero. Then set a[i] = a[i] + x and increment the count for the new value. The update is O(1) on average.
How do I prepare for this in 48 hours?+
Practice two-sum style counting with hash maps, then add updates to a frequency map. Write the query loop once by hand, test on the sample where the answers are [1,2], and check edge cases like repeated updates to one index and duplicate values.