15. 3Sum
mediumAsked at LinearFind all unique triplets in an array that sum to zero. Linear asks this to see if you know the 'anchor + two-pointer' pattern and can handle duplicate elimination cleanly — a level above Two Sum that exposes real interview preparation.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Linear loops.
- Glassdoor (2026-Q1)— Consistently cited in Linear SWE onsite reports as a core medium problem.
- Blind (2025-12)— Multiple Linear interview threads list 3Sum as a recurring onsite question.
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
nums = [-1,0,1,2,-1,-4][[-1,-1,2],[-1,0,1]]Example 2
nums = [0,1,1][]Example 3
nums = [0,0,0][[0,0,0]]Approaches
1. Brute force three loops
Check every triplet combination. Use a set to deduplicate results.
- Time
- O(n^3)
- Space
- O(n)
function threeSum(nums) {
const result = new Set();
for (let i = 0; i < nums.length - 2; i++) {
for (let j = i + 1; j < nums.length - 1; 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);
result.add(JSON.stringify(t));
}
}
}
}
return [...result].map(JSON.parse);
}Tradeoff: O(n^3) — correct but unacceptably slow. Establish this as a baseline, then pivot immediately.
2. Sort + anchor + two-pointer (optimal)
Sort the array. For each anchor element nums[i], use two pointers to find pairs in the remaining window that sum to -nums[i].
- Time
- O(n^2)
- Space
- O(1)
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 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, O(1) extra space (excluding output). Sorting enables two-pointer and also makes deduplication easy — skip over duplicate anchor values and duplicate pair values after recording a result.
Linear-specific tips
Linear interviewers expect you to name the pattern upfront: 'Sort, then anchor + two-pointer.' Walk through duplicate-skipping logic verbally before coding it — candidates who code first and explain second often miss the edge cases. Also mention that sorting converts this into a solvable Two Sum variant.
Common mistakes
- Not skipping duplicate anchor values — leads to duplicate triplets in the output.
- Not skipping duplicate pointer values after recording a match — same issue.
- Skipping duplicates in the wrong direction (e.g., checking nums[left] === nums[left + 1] before incrementing left).
- Forgetting to break early when nums[i] > 0 — if the anchor is positive, no triplet can sum to zero.
Follow-up questions
An interviewer at Linear may pivot to one of these next:
- 3Sum Closest (LC 16) — find the triplet closest to target, not exactly zero.
- 4Sum (LC 18) — generalize to four elements (anchor + 3Sum).
- What if you need all unique quadruplets summing to a target?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why sort first?
Sorting lets you use two pointers (O(n) per anchor) instead of a hash set (O(n) per anchor but with extra space). It also makes deduplication trivial — just skip consecutive equal values.
Why break when nums[i] > 0?
After sorting, if the smallest anchor is positive, all three numbers are positive and can't sum to zero. No point continuing.
Can I use a hash set instead of two pointers?
Yes — fix i and j, then look up -(nums[i]+nums[j]) in a set. But deduplication is harder to get right. Two-pointer is cleaner and preferred for this problem.
Practice these live with InterviewChamp.AI
Drill 3Sum and other Linear interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →