Count Pairs
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon asked this in April 2024, and it's a counting problem that looks simpler than it is. You get a dataset and need to find pairs that match some criteria. The trap is assuming a brute-force nested loop will pass. It won't. You're looking at either a hash-table pass to precompute, or a sorting plus two-pointer sweep. StealthCoder will catch you if you blank on the optimization, since the naive read is tempting and the time limit is tight.
Pattern and pitfall
Count Pairs almost always hinges on reducing a quadratic scan to linear or O(n log n). The pattern: read the input once, hash or sort to build a lookup structure, then iterate again to match. Common pitfall is iterating all pairs directly, which times out on large datasets. If the pair condition is additive (sum equals target, difference equals value), hash-table works beautifully. If it's relational (smaller paired with larger, indices matter), sorting plus two-pointers is cleaner. StealthCoder serves as your real-time pattern recognition when you're stuck between approaches during the live OA.
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 Pairs 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
This OA pattern shows up on LeetCode as two sum. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon 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 Pairs FAQ
Is this really just nested loops?+
No. Nested loops will timeout. You need either a hash-table to precompute counts or sort plus two-pointers. Amazon's test cases are sized to punish O(n^2). Build the lookup structure first, then verify pairs in a single pass.
Should I sort the array?+
Depends on the pair condition. If it's algebraic (sums, differences), hashing is faster. If it's relational and order matters, sorting plus two-pointers is more readable and equally fast. Try sorting first if you're unsure.
How do I handle duplicates?+
This is where candidates lose points. If the input has duplicates and you're counting pairs, you need to track frequency, not just existence. Use a hash map with counts, not a set. Watch edge cases where a pair can match with itself.
What's the trick Amazon is testing?+
Not the algorithm itself. It's whether you can spot that a brute force won't work and shift to a one-pass linear or sorted scan. They want to see you optimize, not just code.
Can I solve this in 48 hours before the OA?+
Not mastery, but recognition. Study one hash-table count problem and one two-pointer pair problem (like two-sum variants). That pattern covers 90% of Amazon pair questions. The rest is muscle memory on the day.