TikTok OA Interview Coding Question: 3Sum Closest

13 Views
No Comments

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.

This problem asks for the three numbers in an array whose sum is closest to a given target. The standard solution sorts the array, then fixes one element and uses two pointers to scan the remaining range while updating the closest sum. By comparing each triplet sum with the target and moving pointers accordingly, the algorithm runs in O(n^2) time instead of O(n^3), which is the expected interview-style approach.

END
 0