Data Structures & AlgorithmsModerateTCS NQT · Infosys

Problem: Maximum sum of a subarray with length exactly 3 in [4, −2, 5, 1, 0, 2, −3, 6]. Report the maximum window sum.

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

Correct answer: B. 7

Sliding a window of 3 gives sums 7, 4, 6, 3, −1, 5, so the maximum is 7 (from 4, −2, 5). Kadane's best subarray of any length gives 13, a window of size 2 gives 5 + 1 = 6, and returning the last window instead of the maximum gives 5.

More Coding Interview Problems questions

Exams that ask this topic