Skip to main content

15. 3Sum

mediumAsked at AMD

Find all unique triplets in an array that sum to zero. AMD uses this to test whether candidates can extend a two-pointer pattern and handle deduplication without a set — skills that transfer to collision detection and register-bank conflict resolution in compiler backends.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in AMD loops.

  • Glassdoor (2026-Q1)AMD SWE onsite candidates report 3Sum as a core medium problem appearing in the second coding round.
  • Blind (2025-10)AMD interview prep threads list 3Sum as a two-pointer/sorting medium commonly asked in SWE roles.

Problem

Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0. Notice that the solution set must not contain duplicate triplets.

Constraints

  • 3 <= nums.length <= 3000
  • −10^5 <= nums[i] <= 10^5

Examples

Example 1

Input
nums = [-1,0,1,2,-1,-4]
Output
[[-1,-1,2],[-1,0,1]]

Explanation: Two unique triplets sum to zero.

Example 2

Input
nums = [0,0,0]
Output
[[0,0,0]]

Explanation: Only one unique triplet.

Approaches

1. Sort + Two Pointers

Sort the array. Fix the first element with an outer loop, then use two pointers (left=i+1, right=end) to find pairs summing to -nums[i]. Skip duplicates at each level.

Time
O(n^2)
Space
O(1) excluding output
function threeSum(nums) {
  nums.sort((a, b) => a - b);
  const result = [];
  for (let i = 0; i < nums.length - 2; i++) {
    if (i > 0 && nums[i] === nums[i - 1]) continue; // skip duplicate i
    if (nums[i] > 0) break; // sorted: no pair can cancel a positive pivot
    let left = i + 1, right = nums.length - 1;
    while (left < right) {
      const sum = nums[i] + nums[left] + nums[right];
      if (sum === 0) {
        result.push([nums[i], nums[left], nums[right]]);
        while (left < right && nums[left] === nums[left + 1]) left++;
        while (left < right && nums[right] === nums[right - 1]) right--;
        left++; right--;
      } else if (sum < 0) {
        left++;
      } else {
        right--;
      }
    }
  }
  return result;
}

Tradeoff: O(n^2) time — optimal for this problem (proven lower bound). O(1) extra space. The deduplication skip loops are the tricky part; walk through them carefully.

AMD-specific tips

AMD interviewers appreciate candidates who articulate why O(n^2) is optimal here: the output alone can be O(n^2) triplets, so you can't do better. Explain the three deduplication points: skip repeated pivot (outer loop), skip repeated left after finding a match, skip repeated right after finding a match. Early exit when nums[i] > 0 is a micro-optimization that signals performance instinct.

Common mistakes

  • Forgetting to skip duplicate pivots in the outer loop — produces duplicate triplets.
  • Skipping duplicates before advancing left and right — you skip past the match itself.
  • Using a Set of arrays for deduplication — JS arrays don't compare by value in Sets.
  • Forgetting the early break when nums[i] > 0 — correct but less efficient.

Follow-up questions

An interviewer at AMD may pivot to one of these next:

  • 4Sum (LC 18) — generalize the outer loop to two fixed indices, still O(n^3).
  • 3Sum Closest (LC 16) — find the triplet whose sum is closest to target.
  • How would you parallelize 3Sum across GPU threads for a very large array?

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why sort first?

Sorting enables the two-pointer approach (shrink the window based on whether the sum is too small or too large) and makes deduplication trivial via adjacent-element comparison.

Why can we break when nums[i] > 0?

After sorting, nums[left] >= nums[i] > 0 and nums[right] >= nums[i] > 0, so the sum is always positive. No valid triplet is possible.

Practice these live with InterviewChamp.AI

Drill 3Sum and other AMD interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →