15. 3Sum
mediumAsked at Juniper NetworksFind all unique triplets that sum to zero. Juniper uses 3Sum to test whether candidates can compose sort + two-pointer cleanly and handle duplicate elimination — the same multi-pass filtering logic appears in prefix-list deduplication and routing policy intersection in network management software.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Juniper Networks loops.
- Glassdoor (2026-Q1)— Cited in Juniper SWE onsite reports as a classic medium-difficulty problem testing sort and two-pointer.
- Blind (2025-12)— Juniper threads list 3Sum as a go-to medium problem in technical interviews across multiple teams.
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
nums = [-1,0,1,2,-1,-4][[-1,-1,2],[-1,0,1]]Explanation: Two unique triplets sum to zero.
Example 2
nums = [0,1,1][]Explanation: No triplet sums to zero.
Example 3
nums = [0,0,0][[0,0,0]]Explanation: Only one unique triplet.
Approaches
1. Sort + two pointers
Sort the array. For each element nums[i], use two pointers (left = i+1, right = n-1) to find pairs that sum 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 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²) time, O(1) extra space. The canonical solution. Duplicate skipping is the subtle part — get it right at all three levels (i, left, right).
Juniper Networks-specific tips
Sorting first is the key insight — state it before writing code. The two-pointer sweep works because the sorted order lets you decide directionally whether to increase or decrease the running sum. Juniper interviewers will specifically probe the duplicate-skipping logic. Walk through nums = [-1,-1,0,1] step by step to show you handle the i-level duplicate correctly.
Common mistakes
- Forgetting to skip duplicates at the i level (i > 0 && nums[i] === nums[i-1]) — produces duplicate triplets.
- Skipping duplicates before pushing the result instead of after — misses the first occurrence of a valid triplet.
- Not advancing both pointers after finding a triplet — causes an infinite loop when duplicates exist.
- Using a Set of stringified arrays to deduplicate instead of pointer logic — correct but much slower.
Follow-up questions
An interviewer at Juniper Networks may pivot to one of these next:
- 4Sum (LC 18) — add one more loop and reduce to 3Sum.
- 3Sum Closest (LC 16) — find the triplet sum closest to a target.
- How would you extend to k-Sum for arbitrary k?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why sort first?
Sorting enables the two-pointer technique (move left up, right down based on sum direction) and makes duplicate detection trivial by grouping equal values together.
What does the i > 0 guard on the duplicate skip do?
It prevents skipping the very first element (i=0). Without it, we would skip nums[0] before ever processing it.
Can we do better than O(n²)?
No known general algorithm does better than O(n²) for the exact triplet enumeration problem. O(n log n) for preprocessing (sort) does not help the output phase.
Practice these live with InterviewChamp.AI
Drill 3Sum and other Juniper Networks interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →