Data Structures & AlgorithmsHardProduct company OA · Amazon/Microsoft-style OA

Array a = [2, 1, 5, 2, 3, 1, 4] has only positive values. How many non-empty contiguous subarrays have sum at most 9?

  1. A. 17
  2. B. 10
  3. C. 28
  4. D. 11

Correct answer: A. 17

Positive values make a sliding window valid: for each right end r, r − l + 1 subarrays end at r, totalling 17. Adding r − l instead (forgetting the + 1) gives 10, counting all 28 subarrays ignores the bound, and counting the subarrays that exceed 9 gives 11.

More Coding Interview Problems questions

Exams that ask this topic