Data Structures & AlgorithmsHardProduct placement · Amazon/Microsoft

What is the length of the shortest contiguous subarray of [1, 3, 1, 8, 7, 3, 4, 7, 1, 2] whose sum is at least 20? (All values are positive, so a shrinking sliding window works.)

  1. A. 10
  2. B. 4
  3. C. 3
  4. D. 5

Correct answer: B. 4

Expand right until the sum reaches 20, then shrink from the left while it stays ≥ 20, recording the length each time. The minimum length is 4.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic