Two Sum with Distinct Pairs
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Bloomberg reported this one in March 2020, and it looks like plain Two Sum until you read the output rules. Underneath, it's a pair-finding problem with deduplication: find every distinct value pair that hits the target, smaller value first, sorted. If you've got an OA invite, expect the trap to be duplicates, not the search. The pattern is sort plus two pointers, or a hash set. Know both. If you blank during the live assessment, StealthCoder runs invisibly as a safety net and reads the problem for you. But you can own this one without it.
The problem
Return every distinct pair of values from nums whose sum is target. Use two different array positions. Within each pair return the smaller value first, and sort the returned pairs lexicographically. Duplicate occurrences must not duplicate a value pair. Function twoSumDistinctPairs(nums: int[], target: int) → int[][] Examples Example 1 nums = [1,1,2,2,3,4] target = 4 return = [[1,3],[2,2]] Both value pairs exist; repeated positions do not duplicate them. Example 2 nums = [0,-1,2,-3,1] target = -2 return = [[-3,1]] Only -3 and 1 sum to -2. Constraints 0 <= nums.length <= 10^5. Values and target are 32-bit signed integers.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that the duplicate rule collapses everything to value pairs. Sort the array, then run two pointers from both ends. When the sum matches, record [nums[l], nums[r]], then move both pointers past all equal values on each side. That skip step is what kills duplicates. Example 1 shows why: [2,2] needs two different positions, so it appears once, and [1,3] appears once despite two 1s. Sorting also gives you the lexicographic output order for free, since pairs come out with the smaller value first and ascending. The pitfalls are 32-bit overflow when summing (use a wider type in typed languages), forgetting l < r so one position gets reused, and empty input. Total cost is O(n log n) time. A hash-based version works too, but you'd have to sort the results at the end. If the live OA freezes you, StealthCoder is the hedge, but the two-pointer skip logic is short enough to memorize.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Two Sum with Distinct 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. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Two Sum with Distinct Pairs FAQ
What's the trick in Bloomberg's Two Sum with Distinct Pairs?+
Sort first, then use two pointers from both ends. On a match, save the pair and skip all repeated values on both sides. That skip is the whole deduplication story. Sorting also handles the required lexicographic output order, so you don't need a separate sort of the results.
How hard is this really?+
Easy to medium. The core is Two Sum II with a sorted array. The difficulty is in the details: duplicate skipping, using two different positions for pairs like [2,2], and handling an empty array. Most failures come from missed edge cases, not from the algorithm itself.
Should I use a hash set or two pointers?+
Two pointers after sorting is cleaner here because output order and dedup come naturally. A hash set works in O(n) for lookups, but you still need to dedupe pairs and sort them at the end. With n up to 10^5, both fit. Pick whichever you can code without bugs.
What edge cases should I test before submitting?+
Test an empty array, a single element, all identical values like [2,2,2,2] with target 4 (should return one pair), negative numbers, and extreme 32-bit values where the sum could overflow. Also check that a lone 2 with target 4 doesn't pair with itself.
How do I prepare for this in 48 hours?+
Write Two Sum II and 3Sum from scratch until the duplicate-skipping loop is automatic. Then solve this variant once, returning sorted pairs. Time yourself. If you can code the sort, two pointers, and skip logic in under fifteen minutes, you're ready for this one.