Queue
Process work in first-in, first-out order. Queues are the missing linear-structure foundation behind BFS, buffering, and fair scheduling.
Interactive visualization
Use the controls to connect the idea to concrete operations before diving into the full write-up.
Step 1 / 6
Start empty
A queue processes items in first-in, first-out order.
Why queues matter
Queues model fair processing. The oldest item gets handled first, which is exactly why BFS discovers nodes level by level, schedulers process jobs in arrival order, and streaming systems buffer work without reordering it.
Family
Linear structures
State that flows from one end to the other: lists, deques, stacks, and windows.
Builds on
Foundational
You can start here without another page first.
Unlocks
3 next topics
Use these follow-ups when you want to apply the current idea in a richer setting.
Learning paths
4
This topic appears in curated progressions so you can keep moving without guessing the next step.
Problem
You need to process items in the same order they arrive.
Examples:
- users waiting in line
- tasks waiting for a worker
- nodes waiting for breadth-first exploration
- packets waiting inside a buffer
A stack is wrong because it reverses the order. A queue keeps the oldest item at the front.
Core idea
A queue supports two main operations:
enqueue(x)inserts at the backdequeue()removes from the front
That makes it a FIFO structure: first in, first out.
Why it matters
Queues are simple, but they unlock a surprising amount of the atlas:
- BFS depends on a queue to expand the graph one layer at a time
- Deque generalizes a queue by allowing work at both ends
- Monotonic queue builds on queue order plus a dominance rule
Without a queue, breadth-first processing has no clean data structure behind it.
Implementations
Array with head pointer
Fast and compact if you avoid shifting all elements on every pop.
Circular buffer
The classic fixed-capacity queue. Head and tail wrap around.
Linked list
Useful when dynamic growth matters and you want insertion/removal at both logical ends.
Complexity
| Operation | Time |
|---|---|
| Enqueue | |
| Dequeue | |
| Front |
Common mistakes
- Using an array and calling a full left-shift on every dequeue
- Reaching for a deque when a plain queue would be clearer
- Forgetting that FIFO order is the reason BFS gives shortest paths in unweighted graphs
Key takeaways
- A queue is the canonical FIFO structure
- It is the missing conceptual step between linked lists and graph traversal
- BFS is not just βa graph algorithmβ; it is graph traversal powered by queue discipline
- Deque and monotonic queue both make more sense once the plain queue comes first
Practice problems
| Problem | Difficulty | Key idea |
|---|---|---|
| Number of Recent Calls | π’ Easy | Keep only active items in FIFO order |
| Implement Stack using Queues | π‘ Medium | Understand exactly what FIFO gives you |
| Open the Lock | π‘ Medium | BFS frontier is literally a queue |
Relation to other topics
- Deque extends a queue to both ends
- BFS uses a queue to visit nodes by distance layer
- Monotonic queue preserves FIFO order while deleting dominated candidates
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.
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.
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.
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.
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.
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.
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.
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.
Graph toolkit
Start with graph shape, then layer traversal, connectivity structure, and both single-source and all-pairs path algorithms.
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.
Cache & distribution
Connect local eviction policy with distributed key placement and system-scale cache design.
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.