Data Structures & AlgorithmsHardAmazon · Microsoft coding round

For the array [1, 3, -1, -3, 5, 3, 6, 7, 2, 9], consider every window of 4 consecutive elements. The sum of the maximum of each window is:

  1. A. 45
  2. B. 39
  3. C. 42
  4. D. 38

Correct answer: C. 42

The seven windows have maxima 3, 5, 5, 6, 7, 7, 9, which sum to 42. A deque-based sliding window maximum computes this in O(n); window sizes 3 and 5 would give 45 and 39.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic