Dynamic Pair Sum Queries

Reported by candidates from Boston Consulting Group's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Boston Consulting Group OA. Under 2s to a working solution.
Founder's read

Boston Consulting Group reported this one in July 2026, and it looks like a pair-sum problem until you read the queries. Strip it down and it's a frequency map on b plus point updates to a. You're not searching for pairs. You're counting them, and the counts can be maintained cheaply. If you blank on the setup during the OA, StealthCoder runs invisibly on your desktop as a safety net and hands you the structure in real time.

The problem

You are given two integer arrays a and b, and an array queries whose elements must be processed in order.
Each query has one of two forms:
[0, i, x]: assign x to a[i].
[1, x]: count the pairs of indices (i, j) such that a[i] + b[j] = x.
Return an array containing the counts produced by the type 1 queries, in query order.

Function
solution(a: int[], b: int[], queries: int[][]) → int[]

Examples
Example 1
a = [3,4]
b = [1,2,3]
queries = [[1,5],[0,0,1],[1,5]]
return = [2,1]
Initially, two pairs sum to 5: a[0] + b[1] = 3 + 2 and a[1] + b[0] = 4 + 1.
The update [0, 0, 1] assigns 1 to a[0], so a becomes [1, 4].
For the final query, only a[1] + b[0] = 4 + 1 sums to 5.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: build a hash map of counts for b once. For a type 1 query with target x, loop over a and add freq_b[x - a[i]] for each element. That's O(n) per query and fine if a is small. If a is large, keep a second map of counts for a. Then a type 0 update just decrements the old value's count, increments the new one, and assigns a[i]. A type 1 query then loops over the distinct values in b (or a, whichever is smaller). The pitfall is treating a pair as a value pair instead of an index pair. Duplicates must multiply, so 2 copies of 3 in a and 3 copies of 2 in b gives 6 pairs. Also remember updates are assignments, not additions, so you must read the old a[i] before overwriting it. StealthCoder is the hedge if the live OA clock gets to you and the update bookkeeping slips.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

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 passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Boston Consulting Group's OA.

Boston Consulting Group reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Dynamic Pair Sum Queries FAQ

What's the trick in Dynamic Pair Sum Queries?+

Count, don't search. Store frequencies of b in a hash map. For each type 1 query, sum freq_b[x - a[i]] across a. Duplicates are handled automatically because you add counts, not booleans. Updates only touch a, so b's map never changes.

How hard is this problem really?+

Easy to medium. The logic is a two-sum variant with counts. The difficulty is efficiency under many queries and getting update semantics right. If you've written a frequency map before, you can finish the basic version in minutes.

Do I need a map of a as well as b?+

Only if a is large and queries are many. Keeping a count map for a lets you iterate over distinct values instead of every index. On update, decrement the old value, increment the new one, then assign. It's a small change with a real speedup.

What mistakes break the solution?+

Overwriting a[i] before reading the old value, which corrupts your counts. Counting unique values instead of index pairs. Forgetting that x minus a[i] may not exist in the map, so use a default of zero. Returning results for type 0 queries by accident.

How do I prepare for this in 48 hours?+

Practice two-sum style counting with hash maps, then add point updates to the same structure. Write the brute force first, then the map version. Test with duplicates in both arrays and with an update that sets a value equal to its current one.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Boston Consulting Group.

OA at Boston Consulting Group?
Invisible during screen share
Get it