Data Structures & AlgorithmsHardCodeforces Div2 B · DP warm-up

Problem: A sliding window maximum over an array of size n with window size k is computed with a monotonic deque. What is its time complexity, and why not just recompute the max for every window naively?

  1. A. O(n log k) with the deque, O(n) naively
  2. B. O(n) with the deque (each index pushed/popped once); the naive approach recomputing the max per window is O(nk)
  3. C. O(nk) for both approaches
  4. D. O(n) for both, since the naive approach is already optimal

Correct answer: B. O(n) with the deque (each index pushed/popped once); the naive approach recomputing the max per window is O(nk)

The deque keeps indices in decreasing value order and each index is added and removed at most once, giving O(n) total; naively scanning all k elements for every one of the (n-k+1) windows costs O(nk).

More Coding Interview Problems questions

Exams that ask this topic