35. 3Sum
mediumAsked at SalesforceFind all unique triplets in an array that sum to zero. Salesforce uses this to test sort + two-pointer + dedup composition — a real Salesforce engineer must combine all three.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Salesforce loops.
- Glassdoor (2026-Q1)— Recurring on Salesforce backend onsites — they grade dedup carefully.
- Blind (2025-09)— Specifically tests the sort + two-pointer hybrid.
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 = [][]Example 3
nums = [0][]Approaches
1. Brute force triple loop
Try every triple; dedup with stringified-sorted-triple set.
- Time
- O(n^3)
- Space
- O(unique triplets)
function threeSum(nums) {
const seen = new Set();
const result = [];
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 trip = [nums[i], nums[j], nums[k]].sort((a, b) => a - b).join(',');
if (!seen.has(trip)) { seen.add(trip); result.push(trip.split(',').map(Number)); }
}
}
}
}
return result;
}Tradeoff: Cubic. TLE on 3000 elements. Salesforce wants O(n^2).
2. Sort + fix one + two-pointer for the rest
Sort. For each i, two-pointer l/r in (i, n) to find pairs summing to -nums[i]. Skip duplicates at each level.
- Time
- O(n^2)
- 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;
let l = i + 1, r = nums.length - 1;
while (l < r) {
const sum = nums[i] + nums[l] + nums[r];
if (sum === 0) {
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--;
} else if (sum < 0) l++;
else r--;
}
}
return result;
}Tradeoff: O(n^2) time. The three dedup checks (one for i, two for l and r) are essential and the #1 source of bugs.
Salesforce-specific tips
Salesforce grades this on dedup correctness — most candidates get the sum logic right but produce duplicate triplets. Bonus signal: explain why you must skip nums[i] duplicates and also skip nums[l]/nums[r] duplicates AFTER finding a triplet (otherwise [-2, 0, 0, 2, 2] over-emits).
Common mistakes
- Skipping i duplicate at i+1 vs i-1 — using i+1 misses the FIRST duplicate.
- Not skipping l and r duplicates after finding a triplet — emits duplicate solutions.
- Using the brute-force dedup-via-string approach in O(n^3) instead of sort+two-pointer.
Follow-up questions
An interviewer at Salesforce may pivot to one of these next:
- 3Sum Closest (LC 16).
- 4Sum (LC 18).
- 3Sum Smaller (LC 259) — count triplets summing to less than target.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why skip i duplicates with `if (i > 0 && nums[i] === nums[i-1])`?
Because if nums[i] equals the previous, the two-pointer scan for (i, n) is a subset of the previous scan — same triplets, just shifted. Skipping avoids duplicates.
Why two more dedup loops after finding a triplet?
To skip over runs of equal values at l and r. Otherwise the next iteration finds the same triplet again.
Practice these live with InterviewChamp.AI
Drill 3Sum and other Salesforce interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →