Deque (Double-Ended Queue)
Push and pop from both ends in O(1). A deque can behave like a queue, a stack, or a monotonic window depending on how you use it.
Interactive visualization
Use the controls to connect the idea to concrete operations before diving into the full write-up.
Queue view
Push at the back, pop at the front.
Stack view
Push and pop on the same end when LIFO behavior is what you need.
Family
Linear structures
State that flows from one end to the other: lists, deques, stacks, and windows.
Builds on
1 topic
Read these first if you want the surrounding context.
Unlocks
5 next topics
Use these follow-ups when you want to apply the current idea in a richer setting.
Learning paths
2
This topic appears in curated progressions so you can keep moving without guessing the next step.
Problem
A queue gives you efficient work at one end. A stack gives you efficient work at one end too - but not the same end.
Sometimes you need both.
You want a structure that supports:
- push front
- push back
- pop front
- pop back
all in time.
That structure is a deque.
Intuition
Deque stands for double-ended queue.
Think of it as the general container, and then view other structures as restrictions:
- Queue = push back, pop front
- Stack = push and pop on the same end
This is why deques show up everywhere: they are flexible enough to model both FIFO and LIFO behavior, plus a few patterns that neither stacks nor queues can handle alone.
Core operations
push_front(x)
push_back(x)
pop_front()
pop_back()
peek_front()
peek_back()
Every one of these should be .
How to implement it
Circular buffer
Use a fixed-size array (or dynamically resized array) and two indices:
frontback
Wrap them around with modulo arithmetic.
This is cache-friendly and often the fastest implementation in practice.
Doubly linked list
Store pointers both ways:
Node {
val
prev
next
}
Keep pointers to the head and tail. Then all four end operations are pointer rewrites.
This is conceptually simple, but has more pointer overhead than an array-backed deque.
Why a deque matters
0-1 BFS
If every edge weight is 0 or 1, you can replace Dijkstra’s priority queue with a deque:
- weight 0 -> push front
- weight 1 -> push back
That gives you linear time, .
Monotonic queue
Sliding-window maximum/minimum problems use a deque whose values stay monotonic. The front is always the best candidate for the current window, while old indices fall off naturally as the window moves.
Palindrome / two-ended processing
Any time input can be consumed from both sides, a deque is the natural fit.
Undo/redo and task buffers
Systems code often needs to add urgent work to the front while leaving normal work at the back. A deque is the simplest structure for that.
Queue vs stack vs deque
| Structure | Push | Pop | Typical use |
|---|---|---|---|
| Queue | Back | Front | BFS, scheduling |
| Stack | Same end | Same end | DFS, recursion simulation |
| Deque | Front or back | Front or back | 0-1 BFS, sliding windows, hybrid scheduling |
Complexity
| Operation | Time |
|---|---|
| Push front/back | |
| Pop front/back | |
| Peek front/back | |
| Space |
Key takeaways
- A deque is the generalization of both queues and stacks
- If a problem needs efficient work at both ends, a deque should be one of your first guesses
- 0-1 BFS is the signature deque algorithm: front for weight-0 edges, back for weight-1 edges
- The monotonic queue pattern is just a deque plus an ordering invariant, analogous to monotonic stacks
- Array-backed deques are usually the fastest practical implementation; linked lists buy flexibility at the cost of locality
Practice problems
| Problem | Difficulty | Key idea |
|---|---|---|
| Design Circular Deque | 🟡 Medium | Implement both-end operations cleanly |
| Sliding Window Maximum | 🔴 Hard | Monotonic deque keeps the best candidate at the front |
| Shortest Path in Binary Matrix | 🟡 Medium | Standard queue-style BFS; good contrast with 0-1 BFS |
| Jump Game VI | 🔴 Hard | Deque maintains the best DP candidate inside a window |
| Open the Lock | 🟡 Medium | Queue behavior as a restricted deque |
Relation to other topics
- Linked lists provide one of the cleanest conceptual implementations of a deque
- Stacks and queues are both restricted views of the same structure
- BFS uses queue behavior; 0-1 BFS upgrades that queue into a deque
- Two pointers & sliding window often turn into monotonic-deque problems when you also need max/min queries inside the window
Build on these first
These topics supply the mental model or underlying structure that this page assumes.
What this enables
Once the current idea feels natural, these are the most useful next jumps.
BFS — Breadth-First Search
Explore a graph level by level using a queue. BFS finds the shortest path in unweighted graphs and is fundamental to many graph algorithms.
0-1 BFS
Find shortest paths in graphs whose edge weights are only 0 or 1 using a deque instead of a heap.
Stack & Monotonic Stack
LIFO data structure for expression evaluation, backtracking state, and the powerful monotonic stack pattern for next-greater-element problems.
Two Pointers & Sliding Window
Shrink O(n²) brute force to O(n) by maintaining a window or two converging pointers. The go-to technique for subarray and subsequence problems.
Monotonic Queue
Maintain sliding-window candidates in sorted order inside a deque so each window maximum or minimum is available in O(1).
Related directions
These topics live nearby conceptually, even if they are not direct prerequisites.
BFS — Breadth-First Search
Explore a graph level by level using a queue. BFS finds the shortest path in unweighted graphs and is fundamental to many graph algorithms.
0-1 BFS
Find shortest paths in graphs whose edge weights are only 0 or 1 using a deque instead of a heap.
Linked List Operations
Reverse, detect cycles, merge sorted lists. Linked lists test your pointer manipulation skills — every operation is about rewiring next pointers.
Stack & Monotonic Stack
LIFO data structure for expression evaluation, backtracking state, and the powerful monotonic stack pattern for next-greater-element problems.
More from Linear structures
Stay in the same family when you want parallel variations of the same mental model.
Linked List Operations
Reverse, detect cycles, merge sorted lists. Linked lists test your pointer manipulation skills — every operation is about rewiring next pointers.
Queue
Process work in first-in, first-out order. Queues are the missing linear-structure foundation behind BFS, buffering, and fair scheduling.
Stack & Monotonic Stack
LIFO data structure for expression evaluation, backtracking state, and the powerful monotonic stack pattern for next-greater-element problems.
Two Pointers & Sliding Window
Shrink O(n²) brute force to O(n) by maintaining a window or two converging pointers. The go-to technique for subarray and subsequence problems.
Paths that include this topic
Follow one of these sequences if you want a guided next step instead of open-ended browsing.
Weighted shortest paths
See the spectrum from plain BFS to deque-based 0-1 BFS to priority-queue shortest paths and dense all-pairs DP.
Linear toolkit
Learn how sequence-shaped data unlocks queues, stacks, windows, and fast pointer manipulations.
From the blog
Pair the atlas with recent writing from the rest of the site when you want a broader engineering perspective alongside the topic graph.