Interview's MateDSA Visualizer
Interview's Mate

DSA Visualizer

Learn by watching algorithms run

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
All topics
PROBLEM-SOLVING TECHNIQUES

Monotonic Queue

A deque that keeps the window’s maximum or minimum at the front.

Advanced5 problems2 interactive subtopicsO(n) time · O(k) space
Practice in AI DSA
WalkthroughOverviewRecognize itMistakes & edge casesKnowledge check (3)Practice
Algorithms & custom examples (2)Open in full playground →
STEP 1/5First window [1, 3, −1]
window [1, 3, −1] · max 3
01234567
1
3
−1
−3
5
3
6
7
i
out
3
0
DEQUE VALUES, FRONT FIRST
3
−1
  • inside the window
  • active this step
  • result
Space Play←→ StepR Replay
1 / 5
PYTHONnums = [1, 3, -1, -3, 5, 3, 6, 7], k = 3
1from collections import deque
2
3def max_sliding_window(nums, k):
4 dq, out = deque(), []
5 for i, x in enumerate(nums):
6 while dq and nums[dq[-1]] < x:
7 dq.pop()
8 dq.append(i)
9 if dq[0] <= i - k:
10 dq.popleft()
11 if i >= k - 1:
▶12 out.append(nums[dq[0]])
13 return out
STEP 1 OF 5Setup

First window [1, 3, −1]

3 pushed 1 out from the back. Deque values: 3, −1. The max is 3.

VARIABLES

i
2
deque values
[3, -1]
out
[3]
02

Intuition & complexity

The core mental model, trade-offs, and Big-O cost.

What it is

Store indices in a deque with decreasing values. Pop smaller values from the back when a new one arrives, and pop the front when it slides out of the window.

Why it matters

Sliding window maximum drops from O(n × k) to O(n), because each index enters and leaves the deque once.

Think of it like this

A leaderboard that only keeps players who could still become number one before they age out.

Use it when

  • Maximum or minimum of every window of size k
  • DP that needs the best value in a sliding range
  • Shortest subarray with sum at least k (with prefix sums)

Avoid it when

  • Fixed windows where a running sum is enough
  • You need the median - use two heaps

Complexity cheatsheet

OperationTimeSpace
Monotonic dequeO(n)O(k)
Scan each windowO(n × k)O(1)
Heap with lazy deletionO(n log n)O(n)

Often confused with: heaps - they also give the max, but removing expired items is awkward and costs O(log n).

03

How to recognize it

Clues in interview prompts that point straight to this pattern.

sliding window maximumwindow minimumdequewithin k stepsjump within a range
  • The front of the deque is always the current answer
  • Pop the front once its index ≤ i − k
04

Mistakes & edge cases

Catch common off-by-one bugs and corner cases before submitting.

Common mistakes

  • Storing values instead of indices, so you can’t tell when the front expires
  • Forgetting to pop expired indices from the front
  • Mixing < and <= inconsistently with duplicates

Edge cases to test

  • k = 1 - the output equals the input
  • k equals the array length
  • Decreasing input - the deque fills up to k
Interview tip: Contrast it with a heap out loud: same goal, but O(n) instead of O(n log n).
05

Knowledge check

Quick check to lock in the intuition.

Question 1 of 3

What sits at the front of the deque?

06

Practice

Apply what you just watched on real interview problems.

Generate Sliding Window problem in AI DSA Practice →
  • BeginnerSliding Window MaximumLeetCode 239 ↗ (opens in a new tab)
  • IntermediateLongest Continuous Subarray With Absolute Diff Less Than or Equal to LimitLeetCode 1438 ↗ (opens in a new tab)
  • AdvancedMax Value of EquationLeetCode 1499 ↗ (opens in a new tab)
Monotonic StackBacktracking