Dynamic Sum Pair Queries
Reported by candidates from Hudson River Trading's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Hudson River Trading reported this one in September 2026, and the setup is deceptively small: two arrays, a list of queries, and a pair count that has to survive point updates to a. If you're taking this OA soon, the pattern is a frequency map, not nested loops. Example 1 shows it cleanly: sum 5 gives 2 pairs, then one assignment drops it to 1. The brute force is obvious and it will time out. StealthCoder is the safety net if your mind goes blank on the live OA, but the idea here is short enough to own before you sit down.
The problem
You are given two arrays of integers a and b, and an array queries, the elements of which are queries you are required to process. Every queries[i] can have one of the following two forms: [0, i, x]. In this case, you need to assign a[i] the value of x (a[i] = x). [1, x]. In this case, you need to find the total number of pairs of indices i and j such that a[i] + b[j] = x. Perform the given queries in order and return an array containing the results of the queries of the type [1, x]. Function solution(a: int[], b: int[], queries: int[][]) → long[] Examples Example 1 a = [3,4] b = [1,2,3] queries = [[1,5],[0,0,1],[1,5]] return = [2,1] For a = [3, 4], b = [1, 2, 3], and queries = [[1, 5], [0, 0, 1], [1, 5]], the output should be solution(a, b, queries) = [2, 1]. The arrays look like this initially: a = [3, 4] and b = [1, 2, 3] For the query [1, 5], there are two ways to form a sum of 5 using an element from each array: 5 = 3 + 2 = a[0] + b[1] and 5 = 4 + 1 = a[1] + b[0]. So the result is 2. The query [0, 0, 1] re-assigns the value of a[0] to 1, so the arrays now look like this: a = [1, 4] and b = [1, 2, 3] For the final [1, 5] query, there's now only one way to form a sum of 5 using an element from each array: 5 = 4 + 1 = a[1] + b[0]. So the result is 1. Since the two queries of type [1, x] gave results of 2 and 1 respectively, the answer is [2, 1]. Example 2 a = [2,3] b = [1,2,2] queries = [[1,4],[0,0,3],[1,5]] return = [3,4] For a = [2, 3], b = [1, 2, 2], and queries = [[1, 4], [0, 0, 3], [1, 5]], the output should be solution(a, b, queries) = [3, 4]. The arrays look like this initially: a = [2, 3] and b = [1, 2, 2] The remainder of this explanation is cropped from the source image. Constraints FastPrep execution-adapter constraints (not shown in the source image): a and b are non-empty integer arrays. queries is non-empty. Every query is either [0, i, x] or [1, x]. Every update index i is valid for a.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to count values, not indices. Build a hash map of counts for b once, since b never changes. Keep a hash map of counts for a too. For a type 1 query with target x, loop over the distinct values v in a's map and add countA[v] * countB[x - v]. For a type 0 query, decrement the count of the old a[i], increment the count of the new x, and write a[i] = x. The common pitfall is forgetting that duplicates multiply, as Example 2 shows with b = [1,2,2]. Another is overflow: the return type is long, so use 64-bit math. If the query count is large and a has many distinct values, consider flipping the loop to iterate whichever map is smaller. If you freeze mid-OA, StealthCoder can supply the map-based solution while you keep your head clear.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Dynamic Sum Pair 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Hudson River Trading's OA.
Hudson River Trading reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Dynamic Sum Pair Queries FAQ
What's the trick in Dynamic Sum Pair Queries?+
Stop thinking in indices. Store value counts for a and b in hash maps. A sum query becomes a sum of countA[v] * countB[x - v] over distinct values in a. Updates just move one count from the old value to the new one.
How hard is this Hudson River Trading OA question really?+
Medium. The idea is simple once you see counting, but the brute force is the trap. Most of the difficulty is spotting that duplicates multiply and that updates only touch a, so b's map is built once and never changes.
Why does the answer need a long?+
Pair counts multiply, so a sum query can reach roughly len(a) times len(b). With large arrays that overflows a 32-bit int. Accumulate in a 64-bit type and return the results as longs to match the function signature.
What edge cases should I test before submitting?+
Test duplicates in b like Example 2, an update that assigns the same value already present, a target with zero matching pairs, and negative values if allowed. Also make sure you decrement the old value's count before incrementing the new one.
How do I prepare in 48 hours for this kind of question?+
Write the counting-map approach from memory twice. Practice point-update plus aggregate-query problems where a map of frequencies replaces recomputation. Then trace Example 1 by hand, including the update, so the order of decrement and increment is automatic.