Answer: Tesla OA Question 2024 May | Two Pointers & Sorting | on-campus
Answer · Posted Jun 2026
Solution: 3Sum Closest — Sort and Two Pointers Approach A brute force solution check of all possible triplets takes O(N3). We can optimize this to O(N2) by first sorting the array and then using a **Two-Pointer** search for each element: We sort the array. We iterate through the array, fixing the first element nums[i]. We then place two pointers: left = i + 1 and right = nums.length - 1. We compute the sum: sum = nums[i] + nums[left] + ...
The full answer & interview discussion are available to premium members.
Log in Create a free account