Data Structures & AlgorithmsVery hardProduct company OA · Amazon/Microsoft-style OA
Given the array [-1, 2, 1, -4, 6, 3] and target 1, pick three different elements whose sum is as close to 1 as possible. What is that closest sum?
- A. 2
- B. 4
- C. 1
- D. 6
Correct answer: C. 1
Sort, fix the first element, and move two pointers inward depending on whether the sum is below or above the target. The values 2, 3 and -4 sum to exactly 1, so the closest sum is the target itself; a pointer walk that stops early would miss this triple.
More Coding Interview Problems questions
- Heights h = [1, 8, 6, 2, 5, 4, 8, 3, 7, 9] are drawn as vertical lines at positions 0 to 9. What is the largest amount of water that two…
- Sorted positive array a = [1, 2, 3, 4, 6, 8]. How many pairs of indices i < j have a[i] a[j] <= 12?
- Sorted array a = [1, 1, 2, 2, 2, 3, 4, 4]. How many index pairs (i < j) satisfy a[i] + a[j] = 5? Count positions, not distinct value pairs.
- Sorted array a = [-2, -1, -1, 0, 1, 1, 2, 2]. How many DISTINCT value triples (x, y, z) with x <= y <= z taken from the array's values sum…
- Two sorted arrays A = [1, 4, 7, 9] and B = [2, 3, 8, 10, 12] are merged in sorted order with two pointers. What is the 6th smallest…
- Sorted array a = [-3, -1, 0, 2, 4, 5, 9]. How many index pairs i < j have -1 <= a[i] + a[j] <= 5?