35. 3Sum
mediumAsked at VercelGiven an array, return all unique triplets that sum to zero. Vercel asks this for the sort + two-pointer pattern and the dedup logic — both classic interview tests of attention to detail.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Vercel loops.
- Glassdoor (2025-Q4)— Vercel platform onsite; dedup is the bonus signal.
- Blind (2026-Q1)— Listed in Vercel runtime screen.
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 + Set for dedup
Try every (i, j, k); collect those that sum to 0; use a Set of sorted-triplet strings to dedup.
- Time
- O(n^3)
- Space
- O(n) for dedup
function threeSum(nums) {
const seen = new Set();
const out = [];
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).join(',');
if (!seen.has(t)) { seen.add(t); out.push(t.split(',').map(Number)); }
}
}
}
}
return out;
}Tradeoff: Cubic. Mention to motivate the sort + two-pointer.
2. Sort + fix one, two-pointer (optimal)
Sort. For each i, two-pointer on [i+1, n-1] looking for -nums[i]. Skip duplicates at i, l, and r to dedup.
- Time
- O(n^2)
- Space
- O(1) or O(n) for sort
function threeSum(nums) {
nums.sort((a, b) => a - b);
const out = [];
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) {
out.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 out;
}Tradeoff: O(n^2) with O(1) extra space (excluding sort). The dedup at i, l, r is the critical detail — miss it and you get duplicate triplets.
Vercel-specific tips
Vercel grades the dedup logic. The skip-i loop at the top of each outer iteration AND the inner skip-l/skip-r after a match are both required. Bonus signal: noting that you can early-exit when nums[i] > 0 (no way three positives sum to 0).
Common mistakes
- Forgetting the i-dedup (skip if nums[i] === nums[i-1]) — produces duplicate triplets like [-1,-1,2] twice.
- Forgetting the l/r dedup after a match — same issue inside one outer iteration.
- Sorting after collecting triplets — wastes work; sort once at the start.
Follow-up questions
An interviewer at Vercel may pivot to one of these next:
- 3Sum Closest (LC 16) — find the triplet closest to target.
- 4Sum (LC 18) — two nested fixed indices + two-pointer.
- 3Sum with multiplicity (LC 923).
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why three dedup loops?
Each fixed index can be repeated. After fixing i, the same i value would produce the same triplets. After matching at (l, r), advancing both must skip equal values to avoid duplicates within the same i.
Why sort first?
Sorting enables (a) two-pointer narrowing based on sum-vs-target, and (b) easy dedup via 'skip if equal to previous.'
Practice these live with InterviewChamp.AI
Drill 3Sum and other Vercel interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →