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?

  1. A. 2
  2. B. 3
  3. C. 4
  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

Exams that ask this topic