Implement two-pointer unique-pair sum search
Company: Citadel
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: Implement two-pointer unique-pair sum search evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.
Constraints
- 0 <= len(nums) <= 10^5
- nums is sorted in nondecreasing order
- -10^9 <= nums[i], target <= 10^9
- Each unique VALUE pair [a, b] with a <= b must appear at most once
- Use O(1) extra space beyond the returned list (no hash set)
Examples
Input: ([1, 2, 3, 4, 5, 6], 7)
Expected Output: [[1, 6], [2, 5], [3, 4]]
Explanation: Three pairs sum to 7; emitted in increasing order of the smaller element.
Input: ([1, 1, 2, 2, 3, 3], 4)
Expected Output: [[1, 3], [2, 2]]
Explanation: Despite duplicate 1s and 3s, the value pair [1, 3] is reported once; [2, 2] uses two distinct equal elements.
Input: ([2, 2, 2, 2], 4)
Expected Output: [[2, 2]]
Explanation: All elements equal; only the single unique value pair [2, 2] is returned, not one per duplicate.
Input: ([], 5)
Expected Output: []
Explanation: Empty array — no pairs possible.
Input: ([3], 6)
Expected Output: []
Explanation: A single element cannot form a pair (need i < j).
Input: ([-3, -1, 0, 1, 2, 4], 1)
Expected Output: [[-3, 4], [-1, 2], [0, 1]]
Explanation: Works with negatives; the two-pointer logic is unchanged.
Input: ([1, 2, 3, 9, 10], 100)
Expected Output: []
Explanation: No pair reaches the target; pointers converge with no matches.
Input: ([0, 0, 0, 0], 0)
Expected Output: [[0, 0]]
Explanation: Target 0 with all zeros yields exactly one unique pair [0, 0].
Hints
- Because the array is sorted, you don't need a hash set: start one pointer at the left end and one at the right end, and move them toward each other based on whether the current sum is too small or too large.
- When nums[i] + nums[j] == target, record the pair, then skip over ALL duplicates of both nums[i] and nums[j] before continuing — this is what prevents duplicate value pairs.
- If the sum is less than target, the only way to grow it is to move the left pointer right; if it's greater, move the right pointer left. The pointers never cross, giving O(n).