Skip to main content

35. 3Sum

mediumAsked at Reddit

Find all unique triplets that sum to zero. Reddit uses this to test sort + two-pointer + dedup — the same triple-key correlation used in their abuse-detection to find triple-coincidence patterns (IP + user-agent + timing).

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

Source citations

Public interview reports confirming this problem appears in Reddit loops.

  • Glassdoor (2026-Q1)Reddit phone-screen medium, sometimes onsite.
  • Blind (2025-12)Reported on Reddit fraud-detection rounds.

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]]

Example 2

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

Example 3

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

Approaches

1. Three nested loops + dedup via set

Try all (i, j, k) and use a set of sorted triplets to dedup.

Time
O(n^3)
Space
O(n)
function threeSum(nums) {
  const set = new Set();
  for (let i = 0; i < nums.length; i++) {
    for (let j = i + 1; j < nums.length; j++) {
      for (let k = j + 1; k < nums.length; k++) {
        if (nums[i] + nums[j] + nums[k] === 0) {
          set.add([nums[i], nums[j], nums[k]].sort((a,b)=>a-b).join(','));
        }
      }
    }
  }
  return [...set].map(s => s.split(',').map(Number));
}

Tradeoff: Cubic. TLE for n=3000.

2. Sort + two-pointer (optimal)

Sort. Fix i; use two-pointer on [i+1, n) to find pairs summing to -nums[i]. Skip duplicates.

Time
O(n^2)
Space
O(1) extra (sort in-place)
function threeSum(nums) {
  nums.sort((a, b) => a - b);
  const result = [];
  for (let i = 0; i < nums.length - 2; i++) {
    if (nums[i] > 0) break;
    if (i > 0 && nums[i] === nums[i - 1]) continue;
    let l = i + 1, r = nums.length - 1;
    while (l < r) {
      const sum = nums[i] + nums[l] + nums[r];
      if (sum === 0) {
        result.push([nums[i], nums[l], nums[r]]);
        while (l < r && nums[l] === nums[l + 1]) l++;
        while (l < r && nums[r] === nums[r - 1]) r--;
        l++; r--;
      } else if (sum < 0) l++;
      else r--;
    }
  }
  return result;
}

Tradeoff: O(n^2). The dedup skips are the trickiest part.

Reddit-specific tips

Reddit interviewers explicitly grade on the dedup logic — both the outer loop's skip-equal-i and the inner skip-duplicates after finding a match. Bonus signal: connect to detecting triple-coincidence in abuse signals (same IP + UA + timestamp triple).

Common mistakes

  • Forgetting to sort first.
  • Not skipping duplicate i (returns duplicate triplets).
  • Not skipping duplicate l/r (returns duplicate triplets even after sort).

Follow-up questions

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

  • 3Sum closest (LC 16) — sum closest to target.
  • 4Sum (LC 18) — extra outer loop.
  • 3Sum smaller (LC 259) — count triplets with sum < target.

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 early break on nums[i] > 0?

Sorted means all later i are >= 0; with two more non-negatives the sum can't be 0 (unless all zero). Slight speedup.

Could we use a hash set for the inner loop?

Yes — O(n^2) time, but harder to dedup cleanly. Two-pointer is canonical.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →