Skip to main content

15. 3Sum

mediumAsked at Glean

Glean uses 3Sum to evaluate whether candidates can reduce a naive O(n³) problem to O(n²) by sorting and applying two pointers — the same space-time reasoning that shows up in multi-term query intersection optimization.

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

Source citations

Public interview reports confirming this problem appears in Glean loops.

  • Glassdoor (2026-Q1)Reported as a medium staple in Glean SWE onsites — frequently paired with a deduplication discussion.
  • Blind (2025-11)Multiple Glean SWE candidates cite 3Sum as a high-frequency medium that tests two-pointer patterns after sorting.

Problem

Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, 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: Sort to [-4,-1,-1,0,1,2]. Fix -1 at index 1, use two pointers on [-1,0,1,2] to find -1+2=1 and 0+1=1.

Example 2

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

Explanation: No three numbers sum to 0.

Example 3

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

Explanation: Only one unique triplet.

Approaches

1. Sort + two pointers

Sort the array. Fix each element nums[i] as the first of the triplet. Use left and right pointers on the remaining suffix to find pairs summing to -nums[i]. Skip duplicates at each level.

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 dup fixed element
    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²) time, O(1) extra space (excluding output). The canonical answer. Deduplication at both the fixed pointer and the two-pointer levels is the trickiest part.

Glean-specific tips

Before coding, state the plan: 'Sort first — O(n log n) — then for each fixed element, run Two Sum with two pointers in O(n), giving O(n²) total.' Glean interviewers want to hear the reduction from brute-force to the optimized approach verbally. Pay special attention to duplicate skipping — missing that is the most common mistake and Glean tests it with inputs like [-1,-1,0,0,1,1].

Common mistakes

  • Not skipping duplicate values of the fixed pointer nums[i] — produces duplicate triplets in the output.
  • Skipping duplicates before advancing both pointers after finding a zero-sum triplet — causes an infinite loop or missed results.
  • Using a set to deduplicate the output instead of skipping during traversal — works but adds hidden O(n) overhead per triplet.
  • Starting the two-pointer window at 0 instead of i+1 — can reuse the same index as the fixed element.

Follow-up questions

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

  • 3Sum Closest (LC 16) — find the triplet with sum closest to a target.
  • 4Sum (LC 18) — extend to four numbers; O(n³) using the same pattern.
  • How does sorting change the approach when the array is already sorted?

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 help with deduplication?

Duplicates become adjacent after sorting. You can skip them with a simple equality check against the previous element, avoiding the need for a set.

Can you skip nums[i] > 0 early?

Yes — if the fixed element is positive, all remaining elements are also positive (array is sorted), so their sum can never be zero. Adding an early break when nums[i] > 0 is a clean optimization.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →