Three Sum Closest
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The tie-break rule is what sinks people on this Amazon OA, reported September 2026. Three Sum Closest looks like the textbook version, but this one says if two sums are equally close, return the smaller. Most memorized solutions skip that line and fail a hidden test. The pattern is sort plus two pointers, O(n^2) on up to 1000 elements. If you blank on the tie-break or the pointer movement during the live OA, StealthCoder sits invisibly on your screen as a safety net and hands you the working solution.
The problem
Given an integer array nums and an integer target, choose three distinct indices and return the sum of their values that is closest to target. If two obtainable sums are equally close, return the smaller sum. Function threeSumClosest(nums: int[], target: int) → int Examples Example 1 nums = [-1,2,1,-4] target = 1 return = 2 The closest sum is -1 + 2 + 1 = 2. Example 2 nums = [0,0,0] target = 1 return = 0 The only possible sum is 0. Example 3 nums = [-2,0,2,4] target = 1 return = 0 Sums 0 and 2 are both one away, so the smaller sum 0 is returned. Constraints 3 <= nums.length <= 1000. -10^4 <= nums[i], target <= 10^4. The answer fits in a signed 32-bit integer.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Sort the array. Fix index i, then run two pointers lo and hi over the rest. Compute the sum. If it's closer to target than your best, update. The twist here: if the distance equals the best distance, take the smaller sum. So the update condition is smaller distance, or equal distance with a smaller sum. Then move lo up if the sum is below target, hi down if above. If the sum equals target, return it immediately. The common pitfall is copying the classic solution with a strict less-than check, which keeps whichever tie came first. Example 3, [-2,0,2,4] with target 1, exists to catch exactly that: 0 and 2 are both one away, and you must return 0. Another pitfall is brute force O(n^3), which is too slow at n=1000. StealthCoder is your hedge in the live OA if the tie logic slips under pressure.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Three Sum Closest cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as 3sum closest. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Three Sum Closest FAQ
What's the trick in Three Sum Closest?+
Sort the array, fix one index, and sweep two pointers inward on the remainder. Track the sum with the smallest absolute difference to target. Move the left pointer when the sum is too small and the right when it's too big. That gives O(n^2) time instead of O(n^3).
How does the tie-break change the code?+
When two sums are equally far from target, you return the smaller one. So update your best when the new distance is smaller, or when the distance is equal and the new sum is less than the best. A plain strict less-than check will fail Example 3.
Can I just brute force it?+
With n up to 1000, three nested loops means about 166 million triples in the worst case. That's risky and likely to time out. Sorting plus two pointers drops it to roughly a million operations. Use the pointer approach and don't gamble on brute force.
Do I need to handle duplicates?+
Not for correctness. The problem asks for distinct indices, not distinct values, so [0,0,0] is valid. You can skip duplicate values for speed, but it's optional. Just make sure you never reuse the same index, which the i, lo, hi layout guarantees.
How do I prepare for this in 48 hours?+
Write the sorted two-pointer solution from scratch twice. Then test the three given examples by hand, especially the tie case. Also try all negatives and all equal values. Spend your time on the update condition and pointer movement, since that's where bugs show up.