Python 3.11+
The examples use the standard Python syntax and containers supported by the browser Judge; the Monotonic Queue invariant is independent of a minor Python release.
Verify in Python docs(opens in a new tab)Maintain a deque ordered by value to query window extrema while elements enter and leave. Learn its decision rule, maintained state, Python cost model, and transfer from a focused drill to a full Interview Problem.
Recognize it when
Consider Monotonic Queue when the prompt's constraints and required operations match this shape: Maintain a deque ordered by value to query window extrema while elements enter and leave.

Sign in to save your code and progress across devices. The lesson and problem statement remain public.
A monotonic deque stores live candidate indexes in decreasing value order. The front is always the maximum for the current window.
Before reading an answer, remove front indexes at or before index minus width. Values alone cannot reveal expiration, which is why the deque stores indexes.
from collections import deque
def max_deque_trace(values,width):
candidates=deque(); trace=[]
for index,value in enumerate(values):
while candidates and candidates[0]<=index-width: candidates.popleft()
while candidates and values[candidates[-1]]<=value: candidates.pop()
candidates.append(index); trace.append(list(candidates))
return traceImplement max_deque_trace(values,width). Return deque index contents after processing each index for sliding maximum.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
Before appending a new index, pop back candidates no greater than its value. The new value is both stronger and lives longer, so dominated indexes can never become a future maximum.
from collections import deque
def window_maximums(values,width):
candidates=deque(); result=[]
for index,value in enumerate(values):
while candidates and candidates[0]<=index-width: candidates.popleft()
while candidates and values[candidates[-1]]<=value: candidates.pop()
candidates.append(index)
if index>=width-1: result.append(values[candidates[0]])
return resultImplement window_maximums(values,width). Return the maximum for every complete fixed-width window.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
Use a monotonic queue for extrema over a moving contiguous window in O(n). A heap is more general but needs lazy deletion and O(n log n) or O(n log width). Each deque index enters and leaves at most once.
Say: “The front is the strongest live index. I expire old fronts, remove weaker backs, append the new index, then read the front.” State how equal values are owned and when a complete window begins.
Python 3.11+
The examples use the standard Python syntax and containers supported by the browser Judge; the Monotonic Queue invariant is independent of a minor Python release.
Verify in Python docs(opens in a new tab)Interview bounds depend on the stated representation, input model, and real Python operations.
| Operation | Average | Worst | Interview note |
|---|---|---|---|
| Monotonic Queue decision loop | O(n) | O(n) | Maintain a deque of in-window indexes whose values decrease from front to back. |
O(k) for the focused Maintain Sliding-Window Maximums implementation.
These are the mistakes most likely to survive a happy-path example and fail a boundary case.
Applying the pattern without proving its ordering, monotonicity, or window invariant can silently miss valid candidates.
Prevent it: Write the decision rule beside the loop and verify it against Maintain Sliding-Window Maximums before optimizing.
Avoid
def window_maximums(values, window_size):
passUse instead
from collections import deque
def window_maximums(values, window_size):
candidates = deque()
results = []
for index, value in enumerate(values):
while candidates and candidates[0] <= index - window_size:
candidates.popleft()
while candidates and values[candidates[-1]] <= value:
candidates.pop()
candidates.append(index)
if index >= window_size - 1:
results.append(values[candidates[0]])
return resultsWhere you will hit this: Maintain Sliding-Window Maximums(opens in a new tab)
Never expires old indexes, so a past maximum leaks into later windows.
Prevent it: Use the public tests and preserve this state: Indexes increase from front to back.
Avoid
from collections import deque
def window_maximums(values, window_size):
q=deque(); out=[]
for i,v in enumerate(values):
while q and values[q[-1]]<=v: q.pop()
q.append(i)
if i>=window_size-1: out.append(values[q[0]])
return outUse instead
from collections import deque
def window_maximums(values, window_size):
candidates = deque()
results = []
for index, value in enumerate(values):
while candidates and candidates[0] <= index - window_size:
candidates.popleft()
while candidates and values[candidates[-1]] <= value:
candidates.pop()
candidates.append(index)
if index >= window_size - 1:
results.append(values[candidates[0]])
return resultsWhere you will hit this: Maintain Sliding-Window Maximums(opens in a new tab)
Python slicing, copying, membership in lists, or repeated sorting inside the loop can invalidate the intended complexity.
Prevent it: Count every slice, copy, sort, membership check, and container update before claiming O(n).
Avoid
from collections import deque
def window_maximums(values, window_size):
q=deque(); out=[]
for i,v in enumerate(values):
while q and values[q[-1]]<=v: q.pop()
q.append(i)
if i>=window_size-1: out.append(values[q[0]])
return outUse instead
from collections import deque
def window_maximums(values, window_size):
candidates = deque()
results = []
for index, value in enumerate(values):
while candidates and candidates[0] <= index - window_size:
candidates.popleft()
while candidates and values[candidates[-1]] <= value:
candidates.pop()
candidates.append(index)
if index >= window_size - 1:
results.append(values[candidates[0]])
return resultsWhere you will hit this: Longest Continuous Subarray Within Limit(opens in a new tab)
Official Python documentation supports language behavior. Canonical problem pages provide additional practice context.
python-docs · checked 2026-07-12