DSA Notes
Master the deque (double-ended queue) — a data structure allowing O(1) insertion and deletion at both ends. Learn complete implementation using doubly linked list and circular array, all operations, monotonic deque applications, and classic sliding window maximum problem.
Introduction
A deque (pronounced "deck") stands for Double-Ended Queue. Unlike a regular queue (enqueue at rear, dequeue at front), a deque allows insertion and deletion at both ends — the front and the rear — all in O(1).
A deque is more general than both a stack and a queue. It can simulate a stack (add/remove from same end) or a queue (add to one end, remove from other end).
Python's collections.deque
Python provides a highly optimized deque in collections.deque, implemented as a doubly-linked list of fixed-size blocks:
Custom Deque Implementation — Doubly Linked List
Deque as Stack and Queue
# Use as Stack (LIFO):
stack = Deque()
stack.append(1) # push
stack.append(2)
print(stack.pop()) # pop from right: 2
# Use as Queue (FIFO):
queue = Deque()
queue.append(1) # enqueue at right
queue.append(2)
print(queue.popleft()) # dequeue from left: 1Monotonic Deque — The Sliding Window Maximum
The most important algorithmic use of a deque: maintaining a monotonic deque for sliding window problems.
Problem: Given an array and window size k, find the maximum in every window of size k.
Naive approach: O(n×k) — recompute max for each window
Optimal with monotonic deque: O(n)
Trace:
| i=0, nums[0]=1 | dq=[0] |
| i=1, nums[1]=3 | 1<3, pop 0. dq=[1] |
| i=2, nums[2]=-1 | dq=[1,2]. Window=[1,2]: max=nums[1]=3. result=[3] |
| i=3, nums[3]=-3 | dq=[1,2,3]. Window=[2,3]: max=nums[1]=3. result=[3,3] |
| i=4, nums[4]=5 | -3<5 pop 3, -1<5 pop 2, 3<5 pop 1. dq=[4]. Window=[3,4]: max=5. result=[3,3,5] |
| i=5, nums[5]=3 | dq=[4,5]. Window=[4,5]: max=nums[4]=5. result=[3,3,5,5] |
| i=6, nums[6]=6 | 3<6 pop 5. dq=[4,6]. Index 4 still in window (6-3+1=4). max=6. result=[3,3,5,5,6] |
| i=7, nums[7]=7 | 6<7 pop 6, 5<7 pop 4. dq=[7]. max=7. result=[3,3,5,5,6,7] |
First Negative in Every Window
Deque Applications Summary
| Application | Deque Usage |
|---|---|
| Sliding window maximum | Monotonic decreasing deque |
| Sliding window minimum | Monotonic increasing deque |
| BFS with bidirectional search | Both-end access |
| 0-1 BFS (weighted: 0 or 1) | Prepend weight-0 edges, append weight-1 |
| Palindrome check (iterative) | Compare front and back |
| Undo-redo with history limit | Bounded deque (maxlen) |
| LRU cache (combined with hash map) | Doubly linked list = deque |
| Text editor cursor (left/right) | Two stacks ~ two halves of deque |
Summary
- Deque = double-ended queue: O(1) add/remove from both ends
- More general than stack or queue — can simulate both
- Python's
collections.deque: The standard, optimized implementation — use this by default - Custom implementation: Doubly linked list with sentinel nodes
- Monotonic deque: The key algorithmic pattern — maintains potential extremes for sliding window
maxlendeque: Automatically drops oldest elements — perfect for rolling windows
*Next Lesson: Priority Queue — Elements processed by priority, not arrival order — implemented with heaps.*
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Deque — Double-Ended Queue with Complete Implementation and Applications.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, queues, deque, deque — double-ended queue with complete implementation and applications
Related Data Structures & Algorithms Topics