Skip to main content

35. 3Sum

mediumAsked at Snowflake

Find all unique triplets that sum to zero. Snowflake uses this to test deduplication discipline at multiple levels — the same care needed when implementing DISTINCT inside GROUP BY queries on multiple columns.

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

Source citations

Public interview reports confirming this problem appears in Snowflake loops.

  • Glassdoor (2026-Q1)Snowflake new-grad onsite as DISTINCT semantics warm-up.
  • LeetCode Discuss (2025-12)Recurring at Snowflake SDE-I screens.

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 + set

Try every triplet; dedup via sorted-key string.

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) {
          const t = [nums[i], nums[j], nums[k]].sort((a, b) => a - b);
          set.add(t.join(','));
        }
  return [...set].map(s => s.split(',').map(Number));
}

Tradeoff: Cubic. Mention to reject.

2. Sort + two-pointer with dedup (optimal)

Sort. For each i, use two pointers (l, r) on the rest to find pairs that sum to -nums[i]. Skip duplicates at i, l, and r.

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 (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: n^2, no extra dedup pass — handled inline via the sort + skip-equal logic.

Snowflake-specific tips

Snowflake interviewers grade this on whether you handle dedup at all three levels (i, l, r) without resorting to a hash set. Bonus signal: pivot to multi-column DISTINCT — implementing DISTINCT on (a, b, c) in SQL requires hashing the composite key, which is exactly what naive dedup does here.

Common mistakes

  • Forgetting to dedup at one level (most commonly l or r after a found triplet).
  • Off-by-one on the inner skip-equal loops, advancing past the boundary.
  • Returning indices instead of values.

Follow-up questions

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

  • 3Sum Closest (LC 16).
  • 4Sum (LC 18) — generalize with one more nesting.
  • k-Sum recursive.

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?

Sortedness enables two-pointer convergence AND inline deduplication via skip-equal — both of which would otherwise require O(n) extra space.

Why skip duplicates after a found triplet?

Without it, an input like [-1,-1,-1,2] would emit [-1,-1,2] twice. Skipping equal neighbors at l and r ensures each unique triplet is reported once.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →