Stacks and Queues
collections.deque is the standard Python tool for in-process queues, and a common way to build rate limiters that count recent requests.After this lesson, you will be able to:
- Implement a Stack (LIFO) with push, pop, and peek operations using a list
- Implement a Queue (FIFO) using collections.deque for O(1) enqueue and dequeue
- Explain why deque is more efficient than list for queue operations
- Apply a monotonic stack to solve classic problems like Next Greater Element
- Implement a circular queue and a min-stack that tracks minimum in O(1)
- Use queue.Queue and queue.PriorityQueue for thread-safe operations
Before you start
#Stack (LIFO): Deep Dive
class Stack:
"""A stack data structure (LIFO)."""
def __init__(self):
self._items = []
def push(self, item):
"""Add an item to the top of the stack. O(1) amortized."""
self._items.append(item)
def pop(self):
"""Remove and return the top item. Raises IndexError if empty. O(1)."""
if self.is_empty():
raise IndexError("Pop from an empty stack")
return self._items.pop()
def peek(self):
"""Return the top item without removing it. O(1)."""
if self.is_empty():
raise IndexError("Peek at an empty stack")
return self._items[-1]
def is_empty(self):
"""Check if the stack is empty."""
return len(self._items) == 0
def size(self):
"""Return the number of items in the stack."""
return len(self._items)
def __str__(self):
return f"Stack(top -> {self._items[::-1]})"
# Using the stack
stack = Stack()
stack.push("A")
stack.push("B")
stack.push("C")
print(stack) # Stack(top -> ['C', 'B', 'A'])
print(stack.peek()) # C
print(stack.pop()) # C (removed from top)
print(stack.pop()) # B
print(stack) # Stack(top -> ['A'])append is O(1) amortized: averaged over many calls the cost is constant, even though an occasional call has to grow the list.You push A, B, C onto a stack. What's the first element you pop?
#How the Call Stack Works
RecursionError that appears when too many frames pile up.#Application: Balanced Parentheses Checker
The call stack insight directly maps to bracket matching — nested brackets mirror nested function calls.
def is_balanced(expression):
"""Check if parentheses, brackets, and braces are balanced.
Uses a stack to track opening brackets. When we see a closing
bracket, we pop from the stack and verify it matches.
"""
stack = Stack()
matching = {")": "(", "]": "[", "}": "{"}
for char in expression:
if char in "([{":
stack.push(char)
elif char in ")]}":
if stack.is_empty() or stack.pop() != matching[char]:
return False
return stack.is_empty() # must be empty — all brackets closed
print(is_balanced("((a + b) * [c - d])")) # True
print(is_balanced("((a + b)")) # False — missing closing
print(is_balanced("([)]")) # False — wrong nesting
print(is_balanced("{[()]}")) # True — correct nesting#Application: Evaluate Postfix (RPN) Expressions
3 4 + 2 * means (3 + 4) * 2. Evaluating it requires only a stack:def evaluate_rpn(tokens):
"""Evaluate a postfix expression given as a list of tokens.
Algorithm:
- If token is a number: push it
- If token is an operator: pop two operands, apply, push result
"""
stack = Stack()
operators = {
"+": lambda a, b: a + b,
"-": lambda a, b: a - b,
"*": lambda a, b: a * b,
"/": lambda a, b: int(a / b), # truncate toward zero
}
for token in tokens:
if token in operators:
b = stack.pop() # second operand
a = stack.pop() # first operand
stack.push(operators[token](a, b))
else:
stack.push(int(token))
return stack.pop()
# (3 + 4) * 2 => 3 4 + 2 *
print(evaluate_rpn(["3", "4", "+", "2", "*"])) # 14
# 5 + ((1 + 2) * 4) - 3 => 5 1 2 + 4 * + 3 -
print(evaluate_rpn(["5", "1", "2", "+", "4", "*", "+", "3", "-"])) # 14The operator branch pops into `b` first, then `a`, and calls `operators[token](a, b)`. What breaks if you swap those two pops?
#MinStack: O(1) Minimum at Any Time
push, pop, top, and get_min — all in O(1). The trick is a parallel min stack that tracks the running minimum.class MinStack:
"""A stack that returns the minimum element in O(1).
Key insight: maintain a parallel stack where each position
stores the minimum of all elements at or below that position.
"""
def __init__(self):
self._stack = []
self._min_stack = [] # parallel stack tracking minimums
def push(self, val):
self._stack.append(val)
# push current min (maintain invariant: min_stack[-1] is always current min)
current_min = val if not self._min_stack else min(val, self._min_stack[-1])
self._min_stack.append(current_min)
def pop(self):
self._min_stack.pop()
return self._stack.pop()
def top(self):
return self._stack[-1]
def get_min(self):
return self._min_stack[-1] # O(1)!
ms = MinStack()
ms.push(5)
ms.push(3)
ms.push(7)
ms.push(2)
ms.push(4)
print(ms.get_min()) # 2 (current minimum)
ms.pop() # remove 4
print(ms.get_min()) # 2 (still 2)
ms.pop() # remove 2
print(ms.get_min()) # 3 (minimum is now 3)
ms.pop() # remove 7
print(ms.get_min()) # 3 (still 3)
ms.pop() # remove 3
print(ms.get_min()) # 5 (only 5 remains)Figure
MinStack visualized: two parallel stacks side by side. Left stack shows actual values [5, 3, 7, 2, 4]. Right min_stack shows running minimums [5, 3, 3, 2, 2]. As you pop elements from the left, the corresponding min is also removed from the right. The min_stack top always shows the current minimum in O(1).
Starting from an empty MinStack you push 5, then 3, then 7. What does _min_stack hold?
#Queue (FIFO): Deep Dive
#Why deque, Not list?
collections.deque instead of a plain list?# THE WRONG WAY — list as a queue
bad_queue = []
bad_queue.append("Alice") # O(1) — fast
bad_queue.append("Bob") # O(1) — fast
bad_queue.pop(0) # O(n) — SLOW! Shifts every remaining element left
# THE RIGHT WAY — deque
from collections import deque
good_queue = deque()
good_queue.append("Alice") # O(1) — fast
good_queue.append("Bob") # O(1) — fast
good_queue.popleft() # O(1) — fast! No shifting needed
# Benchmark to see the difference:
import timeit
def list_queue(n):
q = []
for i in range(n):
q.append(i)
for _ in range(n):
q.pop(0) # O(n) each time
def deque_queue(n):
q = deque()
for i in range(n):
q.append(i)
for _ in range(n):
q.popleft() # O(1) each time
n = 10_000
list_time = timeit.timeit(lambda: list_queue(n), number=10)
deque_time = timeit.timeit(lambda: deque_queue(n), number=10)
print(f"List (n={n}): {list_time:.3f}s") # example measurement, one laptop: ~0.15s
print(f"Deque (n={n}): {deque_time:.3f}s") # example: ~0.008s, about 20x faster here; the gap grows with n
print("Deque is faster:", deque_time < list_time) # True; the exact times differ on every machine`bad_queue.pop(0)` is O(n) while `bad_queue.pop()` is O(1). Why does removing from the front cost so much more?
#Queue Implementation
from collections import deque
class Queue:
"""A queue data structure (FIFO) backed by collections.deque."""
def __init__(self):
self._items = deque()
def enqueue(self, item):
"""Add an item to the back of the queue. O(1)."""
self._items.append(item)
def dequeue(self):
"""Remove and return the front item. Raises IndexError if empty. O(1)."""
if self.is_empty():
raise IndexError("Dequeue from an empty queue")
return self._items.popleft()
def front(self):
"""Return the front item without removing it. O(1)."""
if self.is_empty():
raise IndexError("Front of an empty queue")
return self._items[0]
def is_empty(self):
"""Check if the queue is empty."""
return len(self._items) == 0
def size(self):
"""Return the number of items in the queue."""
return len(self._items)
def __str__(self):
return f"Queue(front -> {list(self._items)})"
queue = Queue()
queue.enqueue("Alice")
queue.enqueue("Bob")
queue.enqueue("Charlie")
print(queue) # Queue(front -> ['Alice', 'Bob', 'Charlie'])
print(queue.dequeue()) # Alice (first in, first out)
print(queue.dequeue()) # Bob
print(queue) # Queue(front -> ['Charlie'])#Circular Queue
front and rear) that wrap around using modulo arithmetic. The modulo operator % gives the remainder after division, so (3 + 1) % 4 is 0 and the index jumps back to the start. This avoids wasted space and is the foundation of ring buffers in operating systems and hardware drivers.class CircularQueue:
"""Fixed-capacity queue using a circular array.
Uses modulo arithmetic so the rear index wraps around
to the front of the array when it reaches the end.
Memory: always exactly `capacity` slots — no dynamic resizing.
"""
def __init__(self, capacity):
self._data = [None] * capacity
self._front = 0
self._rear = -1
self._size = 0
self._capacity = capacity
def enqueue(self, val):
if self.is_full():
raise OverflowError("Queue is full")
self._rear = (self._rear + 1) % self._capacity
self._data[self._rear] = val
self._size += 1
def dequeue(self):
if self.is_empty():
raise IndexError("Queue is empty")
val = self._data[self._front]
self._data[self._front] = None # help GC
self._front = (self._front + 1) % self._capacity
self._size -= 1
return val
def peek(self):
if self.is_empty():
raise IndexError("Queue is empty")
return self._data[self._front]
def is_empty(self):
return self._size == 0
def is_full(self):
return self._size == self._capacity
def __str__(self):
return f"CircularQueue(data={self._data}, front={self._front}, rear={self._rear})"
cq = CircularQueue(4)
cq.enqueue("A")
cq.enqueue("B")
cq.enqueue("C")
print(cq) # CircularQueue(data=['A', 'B', 'C', None], front=0, rear=2)
print(cq.dequeue()) # A — front advances to index 1
cq.enqueue("D") # rear wraps: (2+1) % 4 = 3
cq.enqueue("E") # rear wraps: (3+1) % 4 = 0 — wraps to start!
print(cq) # CircularQueue(data=['E', 'B', 'C', 'D'], front=1, rear=0)After that trace, rear is 0 and front is 1 — rear sits behind front in the array. Is the queue broken?
#Thread-Safe Queues: the queue Module
queue module provides this.None on the queue. That is a sentinel, a special value that means "no more work".# Run this one on your own machine: the in-browser Python cannot start threads.
import queue
import threading
import time
# queue.Queue — standard FIFO, thread-safe
# queue.LifoQueue — LIFO (thread-safe stack)
# queue.PriorityQueue — priority-based ordering, thread-safe
# (it is a locked wrapper around a heap; the heap itself is
# covered in the lesson on Heaps)
# ---- Producer-Consumer Pattern ----
task_queue = queue.Queue(maxsize=5) # blocks if full
def producer():
for i in range(8):
task_queue.put(f"task-{i}") # blocks if queue is full
print(f"Produced: task-{i}")
time.sleep(0.1)
task_queue.put(None) # sentinel: signals consumer to stop
def consumer():
while True:
item = task_queue.get() # blocks until item is available
if item is None:
break
print(f" Consumed: {item}")
task_queue.task_done() # signals item processing is done
time.sleep(0.25)
t1 = threading.Thread(target=producer)
t2 = threading.Thread(target=consumer)
t1.start(); t2.start()
t1.join(); t2.join()
# ---- PriorityQueue ----
pq = queue.PriorityQueue()
pq.put((3, "low priority task")) # (priority, item) — lower number = higher priority
pq.put((1, "critical task"))
pq.put((2, "medium priority task"))
while not pq.empty():
priority, task = pq.get()
print(f"[P{priority}] {task}")
# [P1] critical task
# [P2] medium priority task
# [P3] low priority taskThe producer ends by putting `None` on the queue, and the consumer stops when it sees it. Why is a sentinel needed at all — why not just stop when the queue is empty?
#deque: The Swiss Army Knife
collections.deque (pronounced "deck") is a double-ended queue — O(1) operations at both ends. It is not just a queue implementation detail; it is a first-class data structure with unique capabilities. The lesson on the collections module introduced its basics next to Counter, defaultdict and namedtuple; this lesson is where deque is used as a queue.from collections import deque
d = deque([1, 2, 3, 4, 5])
# All four endpoint operations are O(1)
d.append(6) # add to right: [1, 2, 3, 4, 5, 6]
d.appendleft(0) # add to left: [0, 1, 2, 3, 4, 5, 6]
d.pop() # remove right: [0, 1, 2, 3, 4, 5]
d.popleft() # remove left: [1, 2, 3, 4, 5]
print(d) # deque([1, 2, 3, 4, 5])
# rotate(n): move n elements from right to left (positive n)
# or left to right (negative n)
d.rotate(2)
print(d) # deque([4, 5, 1, 2, 3])
d.rotate(-2)
print(d) # deque([1, 2, 3, 4, 5]) — back to original#maxlen: Automatic Sliding Window
maxlen parameter caps the size of a deque, so it keeps only the most recent items. The collections-module lesson builds a sensor-reading window and a browser history buffer with it, so this lesson does not repeat them. The rate limiter later in this lesson does the same job by hand.window = deque(maxlen=5) already holds [12, 15, 14, 18, 20]. You append 22. What happens?
#deque vs list: Performance Comparison
from collections import deque
import timeit
n = 100_000
# Prepending to a list: O(n) per insert — shifts all elements
list_prepend = timeit.timeit(
"lst.insert(0, 0)",
setup=f"lst = list(range({n}))", # built once, outside the timing
number=1000
)
# Prepending to a deque: O(1) per appendleft
deque_prepend = timeit.timeit(
"dq.appendleft(0)",
setup=f"from collections import deque; dq = deque(range({n}))",
number=1000
)
print(f"List prepend (1000 inserts): {list_prepend:.4f}s") # example, one laptop: ~0.08s
print(f"Deque prepend (1000 inserts): {deque_prepend:.5f}s") # example, one laptop: ~0.00004s
print("Deque is faster:", deque_prepend < list_prepend) # True; the exact times differ on every machine
# Summary table of complexities:
#
# Operation | list | deque
# -------------------|--------|-------
# append (right) | O(1)* | O(1)
# pop (right) | O(1) | O(1)
# appendleft (left) | O(n) | O(1)
# popleft (left) | O(n) | O(1)
# Index access [i] | O(1) | O(n)
# len() | O(1) | O(1)
#
# * amortized — occasional resizes are O(n)
# Rule: use list when you need fast random access (list[i]),
# use deque when you need fast both-end operations.You need a structure holding 100,000 items where you mostly read `data[50_000]` at random. Which one, and why?
#Monotonic Stack: Advanced Pattern
> or >= in the pop test). When a new item breaks the order, we pop items until the order is restored. This single-pass technique turns many O(n²) problems into O(n).#Next Greater Element: O(n) with Monotonic Stack
def next_greater_element(nums):
"""For each element, find the next element that is greater.
Returns -1 if no greater element exists.
Naive O(n²): for each element, scan right until finding greater.
Monotonic stack O(n): single pass.
We maintain a decreasing stack of indices.
When nums[i] > nums[stack top], the top has found its answer.
"""
n = len(nums)
result = [-1] * n
stack = [] # indices of elements waiting for their "next greater"
for i in range(n):
# Pop all elements that have found their next greater element
while stack and nums[i] > nums[stack[-1]]:
idx = stack.pop()
result[idx] = nums[i] # nums[i] is the next greater for idx
stack.append(i)
# Any index still in the stack has no greater element -> stays -1
return result
nums = [2, 1, 2, 4, 3, 1, 5, 2]
print(next_greater_element(nums))
# [4, 2, 4, 5, 5, 5, -1, -1]
# ^ ^ ^ ^ ^ ^ ^ ^
# | | | | | | no greater element
# 2's NGE=4, 1's NGE=2, 4's NGE=5, ...
# Trace through the first few steps:
# i=0, nums[0]=2: stack empty -> push 0. stack=[0]
# i=1, nums[1]=1: 1 < 2 -> push 1. stack=[0, 1]
# i=2, nums[2]=2: 2 > nums[1]=1 -> pop 1, result[1]=2; 2=nums[0]=2 -> push 2. stack=[0, 2]
# i=3, nums[3]=4: 4 > nums[2]=2 -> pop 2, result[2]=4; 4 > nums[0]=2 -> pop 0, result[0]=4; push 3. stack=[3]The result for [2, 1, 2, 4, 3, 1, 5, 2] ends ..., -1, -1]. Which elements finish with -1, and why?
#Daily Temperatures
Given a list of daily temperatures, find for each day how many days until a warmer temperature. Return 0 if there is no future warmer day.
def daily_temperatures(temps):
"""Find number of days to wait for a warmer temperature.
Uses a monotonic decreasing stack of indices.
When we find a warmer day, resolve all colder waiting days.
"""
n = len(temps)
result = [0] * n
stack = [] # indices of days waiting for a warmer day
for i in range(n):
while stack and temps[i] > temps[stack[-1]]:
prev = stack.pop()
result[prev] = i - prev # days waited = current index - past index
stack.append(i)
return result
temps = [73, 74, 75, 71, 69, 72, 76, 73]
print(daily_temperatures(temps))
# [1, 1, 4, 2, 1, 1, 0, 0]
# Day 0 (73°): wait 1 day (day 1 is 74°)
# Day 2 (75°): wait 4 days (day 6 is 76°)
# Day 6 (76°): no warmer day -> 0#Largest Rectangle in Histogram
The classic hard problem — find the largest rectangle that fits within a histogram. A monotonic stack makes this O(n):
def largest_rectangle(heights):
"""Find the largest rectangle area in a histogram.
Strategy: use a monotonic increasing stack.
When we encounter a bar shorter than the stack top, we know
the stack top bar cannot extend further right. Calculate its area.
Time: O(n) Space: O(n)
"""
stack = [] # indices — maintains increasing heights
max_area = 0
heights = heights + [0] # sentinel: forces all remaining bars to be processed
for i, h in enumerate(heights):
while stack and heights[stack[-1]] > h:
height = heights[stack.pop()]
# width: from current i to the new stack top (or start if stack empty)
width = i if not stack else i - stack[-1] - 1
max_area = max(max_area, height * width)
stack.append(i)
return max_area
# histogram: [2, 1, 5, 6, 2, 3] (bar index 0..5, one # per unit of height)
#
# 6 | #
# 5 | # #
# 4 | # #
# 3 | # # #
# 2 | # # # # #
# 1 | # # # # # #
# +------------------
# 0 1 2 3 4 5
#
# largest rectangle = 10: height 5, width 2, over the bars at indices 2 and 3
# (height 2 only fits across indices 2..5: width 4, area 8)
print(largest_rectangle([2, 1, 5, 6, 2, 3])) # 10
print(largest_rectangle([2, 4])) # 4All three of these have a `while` loop nested inside a `for` loop, yet each is O(n). How can that be?
#Real-World Applications Showcase
# ============================================================
# 1. Undo / Redo System using Two Stacks
# ============================================================
class TextEditor:
"""Simple text editor with undo/redo using two stacks.
undo_stack: history of previous states
redo_stack: states undone (cleared on new action)
"""
def __init__(self):
self.text = ""
self.undo_stack = []
self.redo_stack = []
def type(self, chars):
self.undo_stack.append(self.text)
self.redo_stack.clear() # new action clears redo history
self.text += chars
def undo(self):
if self.undo_stack:
self.redo_stack.append(self.text)
self.text = self.undo_stack.pop()
def redo(self):
if self.redo_stack:
self.undo_stack.append(self.text)
self.text = self.redo_stack.pop()
def __str__(self):
return f'TextEditor("{self.text}")'
editor = TextEditor()
editor.type("Hello")
editor.type(", World")
print(editor) # TextEditor("Hello, World")
editor.undo()
print(editor) # TextEditor("Hello")
editor.undo()
print(editor) # TextEditor("")
editor.redo()
print(editor) # TextEditor("Hello")type() calls self.redo_stack.clear(). Why must a new edit throw away the redo history?
max_requests timestamps per user. A cheaper cousin, the sliding window counter, keeps only two counts and estimates the rest. The limiter reads time.monotonic(), a clock that never jumps backwards, unlike the wall clock. For a repeatable demo the cell passes in made-up times instead of waiting.# ============================================================
# 2. Rate Limiter using deque (Sliding Window Log)
# ============================================================
from collections import deque
import time
class RateLimiter:
"""Sliding window log: at most `max_requests` per `window_seconds`.
Keeps one timestamp per allowed request in a deque, so memory is
O(max_requests). Old timestamps are dropped from the left.
"""
def __init__(self, max_requests, window_seconds):
self.max_requests = max_requests
self.window = window_seconds
self.requests = deque() # timestamps of recent allowed requests
def is_allowed(self, now=None):
if now is None:
now = time.monotonic() # a clock that never goes backwards
# Remove expired timestamps (older than the window)
while self.requests and now - self.requests[0] > self.window:
self.requests.popleft()
if len(self.requests) < self.max_requests:
self.requests.append(now)
return True
return False # rate limit exceeded
# Allow 3 requests per 7 seconds. One request arrives every 2 seconds,
# and the times are passed in so the output is the same on every run.
limiter = RateLimiter(max_requests=3, window_seconds=7)
for i in range(6):
t = i * 2
allowed = limiter.is_allowed(now=t)
print(f"t={t:2d}s Request {i+1}: {'ALLOWED' if allowed else 'BLOCKED'}")
# t= 0s Request 1: ALLOWED
# t= 2s Request 2: ALLOWED
# t= 4s Request 3: ALLOWED
# t= 6s Request 4: BLOCKED <- 3 requests are inside the last 7 seconds
# t= 8s Request 5: ALLOWED <- the request at t=0 has now expired
# t=10s Request 6: ALLOWED <- the request at t=2 has now expiredThe queue is what makes BFS find the shortest path: nodes leave it in order of their distance from the start. The cell below does not copy a whole path into every queue entry. It remembers each node's parent, the node it was reached from, and walks the parents backwards once at the end. The graphs lesson covers BFS and DFS in full.
# ============================================================
# 3. BFS for Shortest Path (Queue Application)
# ============================================================
from collections import deque
def shortest_path(graph, start, end):
"""Find shortest path between two nodes using BFS.
BFS guarantees the shortest path in an unweighted graph because
it explores nodes in order of increasing distance from the start.
A stack (DFS) would find A path, but not necessarily the shortest.
"""
if start == end:
return [start]
queue = deque([start])
parent = {start: None} # node -> the node we came from; doubles as "visited"
while queue:
node = queue.popleft()
for neighbor in graph.get(node, []):
if neighbor not in parent:
parent[neighbor] = node
if neighbor == end:
path = []
while neighbor is not None: # walk back to the start
path.append(neighbor)
neighbor = parent[neighbor]
return path[::-1]
queue.append(neighbor)
return None # no path found
graph = {
"A": ["B", "C"],
"B": ["A", "D", "E"],
"C": ["A", "F"],
"D": ["B"],
"E": ["B", "F"],
"F": ["C", "E"],
}
print(shortest_path(graph, "A", "F")) # ['A', 'C', 'F']
print(shortest_path(graph, "D", "F")) # ['D', 'B', 'E', 'F']#Code Playground
Tests: Explore stacks, monotonic patterns, and deque!
#Key Takeaways
- A stack is LIFO. Push adds to the top, pop removes from the top. Python's function call stack, undo/redo systems, bracket matching, and DFS all rely on this structure
- A queue is FIFO. Enqueue adds to the back, dequeue removes from the front. Always use
collections.deque, neverlist.pop(0), for O(1) dequeue - Circular queues use modulo arithmetic to wrap indexes in a fixed array — the foundation of ring buffers in OS kernels and hardware drivers
collections.dequesupports O(1) operations at both ends. Themaxlenparameter makes it the perfect sliding window and history buffer. Usedequefor queues,listfor random access- Monotonic stacks solve "next greater/smaller element" style problems in O(n) instead of O(n²) by keeping items in one direction and answering the waiting items on each new arrival
queue.Queue/queue.PriorityQueueadd thread-safety for producer-consumer patterns in multithreaded programs
You push the values 5, 3, 7, 2 onto a MinStack (in that order). What does get_min() return?