Data Structures & AlgorithmsModerateInfosys SP/DSE · Product Company SDE

Problem: For n = 10⁶ readings and window size k = 10⁵, report every window maximum. Which approach fits the limits best?

  1. A. Rescan each window – O(nk)
  2. B. Monotonic deque of indices – O(n) total
  3. C. Sort each window – O(n k log k)
  4. D. Prefix maximum array only – O(n)

Correct answer: B. Monotonic deque of indices – O(n) total

Rescanning costs about 10¹¹ operations. A deque that keeps decreasing values pushes and pops each index once: O(n). A plain prefix-maximum array cannot 'forget' elements that leave the window (it works only for windows starting at 0); a block-wise prefix/suffix maximum trick also gives O(n) but the deque is the standard answer.

More Coding Interview Problems questions

Exams that ask this topic