Data Structures & AlgorithmsModerateTCS NQT · Infosys

Array a = [3, -5, 2, -1, -4, 2, 1, -6, 2]. How many contiguous subarrays of length exactly 4 have a negative sum?

  1. A. 7
  2. B. 6
  3. C. 5
  4. D. 22

Correct answer: B. 6

A fixed-length window is updated by subtracting the element leaving and adding the element entering, so each window sum costs O(1) after the first. Counting windows of length at most 4 changes the answer.

More Coding Interview Problems questions

Exams that ask this topic