Data Structures & AlgorithmsHardProduct company OA · Amazon/Microsoft-style OA
For the positive array a = [2, 3, 1, 2, 4, 3] and S = 7, what is the length of the shortest contiguous subarray whose sum is at least 7?
- A. 2
- B. 3
- C. 4
- D. 5
Correct answer: A. 2
All values are positive, so a window that grows on the right and shrinks on the left while its sum is at least S finds the minimum. The answer 2 is the window [4, 3]; a test with a strict > misses windows whose sum equals S exactly.
More Coding Interview Problems questions
- Sorted array a = [1, 3, 4, 7, 10, 12, 15]. How many pairs of indices i < j satisfy a[j] - a[i] <= 3?
- 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…
- 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…