Coding · stretch-tier · asked at Amazon, Apple, Google, Meta, Microsoft, Netflix
3Sum Closest
Problem
Given an integer array nums and an integer target, pick three elements at distinct positions whose sum is as close as possible to target, and return that sum. Inputs are chosen so that a single sum is strictly closest.
numshas between 3 and 600 elements, each between -1000 and 1000.targetis an integer of magnitude at most 20,000, so it may lie outside the range of achievable sums.
Examples
nums = [3, -2, 5, -1, 7], target = 6→6.3 + (-2) + 5hits the target exactly, so nothing can be closer.nums = [4, 4, 4], target = 9→12. Only one triple exists.nums = [2, 2, 2, 1], target = -50→5. Every sum is far above the target; the smallest one,1 + 2 + 2, is the closest.
Hints
- Fix one element. What does the remaining task look like, and have you solved something similar with a sorted array?
- With the rest sorted, two pointers can move toward the target: if the sum is below it, only moving the left pointer right can help; if above, only moving the right pointer left can.
- Sort, iterate the anchor, run the two-pointer sweep on the suffix, and keep the sum with the smallest absolute difference; stop immediately if the difference is zero.
Last reviewed 8 September 2026
Locked
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Secure payment by Stripe · instant access · refund questions: [email protected]
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.