Skip to main content

15. 3Sum

mediumAsked at Akamai

Find all unique triplets in an array that sum to zero. Akamai asks 3Sum to assess whether candidates can layer sorting on top of a two-pointer scan and handle duplicate pruning cleanly — the same deduplication discipline required in log-aggregation pipelines at scale.

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

Source citations

Public interview reports confirming this problem appears in Akamai loops.

  • Glassdoor (2026-Q1)Akamai onsite reports list 3Sum as a common medium-difficulty coding question in algorithm rounds.
  • Blind (2025-10)Multiple Akamai interview threads cite 3Sum as a canonical two-pointer problem asked in second-round interviews.

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. 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. Duplicates are pruned.

Example 2

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

Explanation: No triplet sums to zero.

Example 3

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

Explanation: The only triplet.

Approaches

1. Sort + two-pointer

Sort the array. For each element nums[i] (fixed as the first of the triplet), use a two-pointer scan of the remaining suffix to find pairs summing to -nums[i]. Skip duplicates at both the outer and inner levels.

Time
O(n²)
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
    let left = i + 1;
    let 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²) time, O(1) extra space. The sorting step is what enables two-pointer and deduplication. Early-exit when nums[i] > 0 is a worthwhile optimization to mention.

Akamai-specific tips

Akamai will probe the deduplication logic. Explain it before writing: 'After sorting, if nums[i] equals nums[i-1], the entire two-pointer sweep would produce identical triplets — so I skip. Same for the inner pointers after recording a result.' Also add the early-exit: 'If nums[i] > 0, all elements to the right are >= 0, so no triplet can sum to zero — we can break.'

Common mistakes

  • Skipping duplicates with nums[i] === nums[i+1] instead of nums[i-1] — skips valid triplets at the start of a duplicate run.
  • Forgetting to advance both pointers after recording a match — infinite loop on arrays with many equal elements.
  • Not sorting first and trying a hash-based dedup — far more complex and error-prone.

Follow-up questions

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

  • 3Sum Closest (LC 16) — return the triplet sum closest to a target value.
  • 4Sum (LC 18) — extend to four elements; generalize to k-sum recursively.
  • How would the complexity change if you needed all unique k-tuples summing to zero?

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 does sorting enable deduplication?

After sorting, identical values are adjacent. A simple comparison with the previous element is sufficient to detect and skip a duplicate pivot — no hash set needed.

Can we do better than O(n²)?

The best known algorithm for 3Sum is O(n²). A sub-quadratic solution would imply a breakthrough in computational geometry (3SUM-hardness). Mention this if the interviewer asks.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →