← Questions

Unique pairs with a target sum

0
AmazonCodingLast asked May 2026

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, check target - x, taking x < y once and x == y only 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

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