Count Index Pairs with a Target Sum
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The whole problem hinges on one hash map, and Bloomberg put it in front of candidates in November 2024. You count index pairs (i,j) with i < j where nums[i] + nums[j] equals the target, and duplicates at different indices count as separate pairs. It looks like Two Sum, but you're counting, not returning. With up to 2 * 10^5 elements, the brute-force double loop dies. If you blank during the live OA, StealthCoder runs invisibly as a safety net and hands you the approach. Know the frequency-map pattern and you won't need it.
The problem
Return the number of index pairs (i,j) with i < j and nums[i] + nums[j] == target. Equal values at different indices form distinct pairs. Function countTargetPairs(nums: int[], target: int) → long Examples Example 1 nums = [1,2,1,1,2] target = 3 return = 6 Each of three 1s pairs with each of two 2s. Constraints 0 <= nums.length <= 2 * 10^5. The answer fits in signed 64-bit range.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Walk the array once and keep a hash map of value to count seen so far. For each x, add map[target - x] to the answer, then increment map[x]. That enforces i < j automatically and handles duplicates, since each earlier matching index is its own pair. Example: [1,2,1,1,2] with target 3 gives 6, three 1s times two 2s. The pitfalls are real. The answer can pass 32-bit range, so use a long accumulator. When x equals target - x, query the map before inserting x, or you'll pair an element with itself. Empty arrays should return 0. Don't sort and two-pointer unless you handle duplicate runs with multiplication, which is more error-prone. Time is O(n), space is O(n). If the pattern slips under pressure, StealthCoder is the hedge on the live OA, but this one is short enough to memorize.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Count Index Pairs with a Target Sum 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 StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg 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.
Count Index Pairs with a Target Sum FAQ
How hard is this Bloomberg OA question really?+
Easy to medium. It's Two Sum with counting instead of indices. If you've seen the hash map approach, you finish in minutes. The traps are overflow and the case where both numbers are equal, not the algorithm.
What's the trick to counting pairs?+
Keep a frequency map of values seen so far. For each element, add the count of its complement, then insert the element. One pass, and i < j holds without extra logic.
Why does the return type being long matter?+
With 2 * 10^5 elements, the pair count can reach about 2 * 10^10 when many values match. That overflows a 32-bit int. Use a 64-bit accumulator in whatever language you pick.
Can I sort and use two pointers instead?+
Yes, but duplicates get messy. When both pointers land on equal values you need combinatorics like n*(n-1)/2, and on unequal values you multiply the run lengths. It works, but the hash map is shorter and safer.
How do I prepare for this in 48 hours?+
Write the one-pass frequency map solution from scratch twice. Then test the edge cases: empty array, all equal values with target double that value, and large counts for overflow. That covers nearly everything this problem can throw at you.