Data Structures & AlgorithmsHardTCS NQT · Product Company SDE

Problem: How many contiguous subarrays of [3, -1, 4, -2, 5, -6, 2, 1] have sum at most 6 when the sum is computed over all non-empty contiguous subarrays?

  1. A. 31
  2. B. 32
  3. C. 34
  4. D. 36

Correct answer: C. 34

Brute force over all 36 subarrays finds 34 with sum ≤ 6. Using < 6 gives 31, a shrinking two-pointer window gives 32 because negative values break its monotonicity, and 36 counts every subarray.

More Coding Interview Problems questions

Exams that ask this topic