15. 3Sum
mediumAsked at Apple3Sum is Apple's introduction to fix-one-then-two-pointer pattern. Sort the array, fix nums[i] as the anchor, then run a two-pointer sweep for the other two values that sum to -nums[i]. The duplicate-skipping is the bug surface Apple specifically grades on.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Apple loops.
- Glassdoor (2026-Q1)— Apple SWE phone-screen reports list 3Sum as a recurring 30-minute array medium with duplicate-handling focus.
- Blind (2025-12)— Apple ICT3/ICT4 reports cite 3Sum as the canonical two-pointer-with-anchor 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]]Explanation: The distinct triplets are [-1,0,1] and [-1,-1,2].
Example 2
nums = [0,1,1][]Explanation: The only possible triplet does not sum up to 0.
Example 3
nums = [0,0,0][[0,0,0]]Approaches
1. Sort + fix one, two-pointer the rest (optimal)
Sort. Loop i from 0 to n-3; skip duplicates at i. Two-pointer l=i+1, r=n-1; on match, push and skip duplicates on both sides. On sum<0 advance l; on sum>0 retreat r.
- Time
- O(n^2)
- Space
- O(log n) or O(n) for sort
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;
if (nums[i] > 0) break;
let l = i + 1, r = nums.length - 1;
while (l < r) {
const sum = nums[i] + nums[l] + nums[r];
if (sum < 0) l++;
else if (sum > 0) r--;
else {
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--;
}
}
}
return result;
}Tradeoff: O(n log n) sort dominated by O(n^2) two-pointer scan. The duplicate skips happen in three places: at i (top of outer loop), and at l/r right after a successful push. Missing any of the three duplicates produces wrong output.
2. Hash set for each pair
For each pair (i, j), look up -nums[i]-nums[j] in a hash set.
- Time
- O(n^2)
- Space
- O(n)
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;
const seen = new Set();
for (let j = i + 1; j < nums.length; j++) {
const need = -nums[i] - nums[j];
if (seen.has(need)) {
result.push([nums[i], need, nums[j]]);
while (j + 1 < nums.length && nums[j] === nums[j + 1]) j++;
}
seen.add(nums[j]);
}
}
return result;
}Tradeoff: Same complexity but extra O(n) space per outer iteration. Two-pointer is cleaner. Apple accepts either.
Apple-specific tips
Apple's grading focus on 3Sum is duplicate handling. List the THREE places duplicates must be skipped before writing code: (1) outer loop at i, (2) inner left pointer after a match, (3) inner right pointer after a match. If you state these three out loud, the interviewer relaxes — that's the rubric criterion they wrote down.
Common mistakes
- Forgetting the i > 0 check before the duplicate skip — produces an IndexError on the first iteration.
- Forgetting to skip duplicates at l and r AFTER a match — produces duplicate triplets in output.
- Skipping the early-exit if nums[i] > 0 — works correctness-wise but wastes time on the sorted suffix.
Follow-up questions
An interviewer at Apple may pivot to one of these next:
- 4Sum (LC 18) — one more outer loop, O(n^3).
- 3Sum Closest (LC 16) — track closest sum instead of exact match.
- 3Sum Smaller (LC 259) — count triplets with sum < target.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why sort first?
Sorting enables the two-pointer sweep (monotonic sum) and makes duplicate-skipping a simple equality check on adjacent elements.
Can we do better than O(n^2)?
No — 3SUM has a conjectured Omega(n^2) lower bound, which is why the two-pointer trick is the textbook answer.
Free learning resources
Curated free links for this problem.
Practice these live with InterviewChamp.AI
Drill 3Sum and other Apple interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →