Identify Number Pairs Adding to Target in Array
Company: Amazon
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: This question evaluates skills in array processing, pair-sum detection, handling duplicate values and multiset counts, and reasoning about algorithmic complexity and correctness.
Constraints
- 0 <= n <= 200000
- -10^9 <= nums[i], target <= 10^9
- Return value-based pairs only; indices are irrelevant
- Include [x, x] only if count(x) >= 2
- Within each pair a <= b; output list sorted lexicographically by (a, b)
Hints
- Count frequencies with a hashmap; iterate unique values x and check if target - x exists.
- To avoid duplicates, only form pairs where x <= target - x.
- If x == target - x, include [x, x] only when frequency[x] >= 2. Alternatively, sort the array and use two pointers while skipping duplicates.