Data Structures & AlgorithmsModerateTCS NQT · Product Company SDE

Problem: The maximum of each window of length 3 in [1, 3, −1, −3, 5, 3, 6, 7] is recorded for all windows. What is the sum of these window maxima?

  1. A. 29
  2. B. 22
  3. C. 38
  4. D. 15

Correct answer: A. 29

The six windows have maxima 3, 3, 5, 5, 6, 7, summing to 29 (a deque keeps it O(n)). Looping over only n − 3 windows misses the last one (22), summing window totals instead of maxima gives 38, and using non-overlapping blocks gives 3 + 5 + 7 = 15.

More Coding Interview Problems questions

Exams that ask this topic