16. 3Sum Closest
mediumAsked at NetflixGiven an array of integers and a target, return the sum of three numbers closest to the target. Netflix asks this as the natural follow-up to 3Sum — they want sort + fix-one + two-pointer with a running 'best distance' check that you can defend.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Netflix loops.
- Glassdoor (2026-Q1)— Netflix onsite reports list this as the array-DSA round when 3Sum is felt to be too easy.
- LeetCode Company Tag (2025-Q4)— Netflix is in the top-3 companies tagged for this problem on public LeetCode aggregators.
Problem
Given an integer array nums of length n and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
Constraints
3 <= nums.length <= 500-1000 <= nums[i] <= 1000-10^4 <= target <= 10^4
Examples
Example 1
nums = [-1,2,1,-4], target = 12Explanation: The sum closest to target is -1 + 2 + 1 = 2.
Example 2
nums = [0,0,0], target = 10Approaches
1. Brute-force triple loop
Try every (i, j, k) triple and track the sum with the smallest absolute distance to target.
- Time
- O(n^3)
- Space
- O(1)
function threeSumClosestBrute(nums, target) {
let best = nums[0] + nums[1] + nums[2];
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++) {
const s = nums[i] + nums[j] + nums[k];
if (Math.abs(s - target) < Math.abs(best - target)) best = s;
}
}
}
return best;
}Tradeoff: Correct but n^3 at n = 500 is 125M operations — borderline TLE. Mention it to motivate the sort + two-pointer reduction.
2. Sort + fix-one + two-pointer (optimal)
Sort the array. For each i, use two pointers l = i+1 and r = n-1. Move pointers based on whether the sum is above or below target; track the best.
- Time
- O(n^2)
- Space
- O(1) extra (O(log n) for sort recursion)
function threeSumClosest(nums, target) {
nums.sort((a, b) => a - b);
let best = nums[0] + nums[1] + nums[2];
for (let i = 0; i < nums.length - 2; i++) {
let l = i + 1, r = nums.length - 1;
while (l < r) {
const s = nums[i] + nums[l] + nums[r];
if (Math.abs(s - target) < Math.abs(best - target)) best = s;
if (s < target) l++;
else if (s > target) r--;
else return s;
}
}
return best;
}Tradeoff: O(n^2) with O(1) extra space. Early return on exact match. The 'move l up if sum < target' invariant works because the array is sorted, so increasing l can only raise s.
Netflix-specific tips
Netflix interviewers will probe two things on this one: (1) Do you sort first? Many candidates try to skip it and end up with O(n^3). (2) Do you handle the 'return early on exact match' case? It's a small optimization but signals you're thinking about the loop's correctness, not just throwing pointers around.
Common mistakes
- Initializing best = 0 or Infinity instead of nums[0] + nums[1] + nums[2] — gives wrong answers when all sums are negative.
- Forgetting to sort, leading to incorrect two-pointer behavior.
- Not breaking early when an exact-match sum is found (loses points in time-pressured loops).
Follow-up questions
An interviewer at Netflix may pivot to one of these next:
- What if you had to return the indices of the three numbers? (Track them alongside the sum.)
- 4Sum Closest — same pattern, one more fixed pointer, O(n^3).
- What if the array were already sorted and very large? (Same algorithm, no sort cost.)
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why sort first? Couldn't I use a hash set like in 3Sum?
Hash set works for exact-sum problems but not closest-sum, because you can't query 'closest hash key' efficiently. Sorting + two-pointer is the standard reduction for closeness queries on sums.
Is the early return on s === target really worth it?
Yes — once you hit an exact match, no other triple can be closer, so returning saves work. Interviewers notice the micro-optimization.
Free learning resources
Curated free links for this problem.
Practice these live with InterviewChamp.AI
Drill 3Sum Closest and other Netflix interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →