DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
Back to Arena
medium
Two Pointer

3Sum Closest

**Problem Statement:** Given the required input arguments, write an efficient algorithm to solve the **3Sum Closest** problem. Implement the required logic as specified by standard definitions for this classic algorithmic challenge. **Hint / Expected Approach:** Sort + two-pointer, track closest absolute diff **Edge Cases to Consider:** - (1) Only three elements - (2) Multiple triplets at same distance - (3) Negative target

Examples

Example 1
Input: [-1,2,1,-4], 1
Output: 2
Explanation: Basic case
Example 2
Input: [0,0,0], 1
Output: 0
Explanation: Only one triplet
Example 3
Input: [1,1,1,0], 100
Output: 3
Explanation: Max possible sum

Constraints

  • ▪1 <= data.length <= 10^5
  • ▪Memory complexity must be O(1) or O(k)
  • ▪Ensure non-overlapping pointer access

Watch Out For Edge Cases

  • ▪Only three elements
  • ▪Multiple triplets at same distance
  • ▪Negative target
Frequently Asked At
GoogleAmazonAdobe