← Questions
Unique pairs with a target sum
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Amazon's technical phone screen, last asked May 2026. It tests hash maps and two pointers.
Given an array of integers and a target, return every distinct pair of values that sums to the target. A pair counts once however many times its values repeat. A value can pair with itself only if it appears at least twice.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
unique_pairs(nums: list[int], target: int) -> list[tuple[int, int]]— each pair as(smaller, larger), the list sorted.
Example
unique_pairs([1, 1, 2, 45, 46, 46], 47) -> [(1, 46), (2, 45)]
unique_pairs([3, 3, 3], 6) -> [(3, 3)]
unique_pairs([-1, 0, 1, 2, -1, -4], 0) -> [(-1, 1)] # one 0 can't pair with itself
unique_pairs([5], 10) -> []
Constraints
-
0 <= len(nums) <= 10^5; values fit in 32-bit integers.
Clarifying questions
- Return values or indices? The pairs or only their count?
- Can a value pair with itself when it appears once?
- Does the output need an order?
What a strong answer covers
- A counter of values; for each distinct
x, checktarget - x, takingx < yonce andx == yonly with a count of at least 2. - Or sort and use two pointers, skipping duplicates on both sides.
- O(n) with hashing or O(n log n) with sorting; O(n) space.
- Tests: all duplicates, negatives, no pairs.
Follow-ups
- Count pairs by index instead of by value.
- Pairs whose sum is at most the target.
- Three values that sum to the target, without duplicate triples.
Where it's asked
- Amazon · Technical phone screen · Last asked May 202660 min · Live coding in a shared editor that does not run code, plus Leadership Principles questions · No AI
What it tests
hash maps · two pointers
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
