15. 3Sum
mediumAsked at AndurilFind all unique triplets in an array that sum to zero. Anduril uses this to test whether you can reduce a multi-pointer problem to sorted-array two-pointer reasoning and rigorously eliminate duplicates — the same discipline required when deduplicating redundant sensor readings in a fusion pipeline.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Anduril loops.
- Glassdoor (2026-Q1)— Reported as a medium-difficulty onsite problem at Anduril for SWE generalist roles.
- Blind (2025-11)— Anduril candidate threads cite 3Sum as a common medium filter for mid-level engineer positions.
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: Sort first: [-4,-1,-1,0,1,2]. Fix -1 at index 1, two-pointer finds (-1,2) and (0,1).
Example 2
nums = [0,1,1][]Explanation: No triplet sums to zero.
Example 3
nums = [0,0,0][[0,0,0]]Explanation: The only triplet sums to zero.
Approaches
1. Sort + two pointers
Sort the array. For each index i, use two pointers (left=i+1, right=end) to find pairs that sum to -nums[i]. Skip duplicates at each pointer.
- Time
- O(n^2)
- Space
- O(1) extra (O(n) for output)
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; // sorted: no triplet can sum to 0
if (i > 0 && nums[i] === nums[i - 1]) continue; // skip duplicate i
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. The duplicate-skip logic is the hardest part — be explicit about why you check i > 0 (to avoid skipping index 0 itself) and when to skip left and right duplicates.
Anduril-specific tips
Anduril interviewers watch for rigorous duplicate elimination. Explain why each skip check has its condition (i > 0 prevents skipping the first anchor; the inner while skips after pushing a result). Draw the two-pointer visualization before coding. Also mention the early-exit optimization: if nums[i] > 0, since the array is sorted, no triplet can sum to 0 — this shows you reason about invariants, not just mechanics.
Common mistakes
- Not sorting first — the two-pointer approach requires sorted input.
- Skipping index 0 when deduplicating i — 'if i > 0 && nums[i] == nums[i-1]' is correct; dropping the 'i > 0' check skips the first valid anchor.
- Advancing left and right inside the 'sum === 0' branch without first skipping duplicates — leads to duplicates in the result.
- Using a Set of stringified triplets as the deduplication mechanism — it works but is O(n) per insert; sorting + skip is cleaner.
Follow-up questions
An interviewer at Anduril may pivot to one of these next:
- 4Sum (LC 18) — generalize the approach with one more outer loop.
- 3Sum Closest (LC 16) — track the closest sum rather than an exact zero.
- How many unique triplets exist in O(n log n) space — can you reduce it further?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why sort first?
Sorting enables two-pointer convergence and makes duplicate detection trivial by grouping equal values adjacently.
Why does the outer loop stop at nums.length - 2?
We need at least two more elements for left and right pointers after index i.
Can I use a hash set instead of two pointers?
Yes, but it's harder to deduplicate correctly and uses O(n) extra space. Two pointers is the cleaner O(n^2) canonical answer.
Practice these live with InterviewChamp.AI
Drill 3Sum and other Anduril interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →