Queues and first-in-first-out order
A queue admits elements at the tail and releases them from the head, preserving arrival order. That property is what makes breadth-first search explore a graph level by level rather than diving down one branch.
The variants matter in interviews: a circular queue reuses a fixed buffer without shifting, a deque allows work at both ends, and a priority queue orders by value rather than arrival — which is a heap, not a queue, underneath.
Time and space complexity
| Operation | Queue | Circular queue | Deque |
|---|---|---|---|
| enqueue | O(1) | O(1) | O(1) both ends |
| dequeue | O(1) | O(1) | O(1) both ends |
| peek front | O(1) | O(1) | O(1) |
| search | O(n) | O(n) | O(n) |
| Storage | O(n) | O(capacity) | O(n) |
How to use this visualizer
Select a queue variant to load its pseudocode.
Step through enqueue and dequeue and watch the head and tail indices move.
On the circular track, watch the tail wrap around the fixed buffer.
Note that the head index advancing is what makes dequeue O(1).
Frequently asked questions
In a plain array queue, dequeuing from index 0 shifts every remaining element left, costing O(n). A circular queue instead advances a head index and wraps it modulo the capacity, so both enqueue and dequeue stay O(1) and the freed slots at the front get reused instead of wasted.
Keep an inbox and an outbox stack. Push every new element onto the inbox. To dequeue, pop from the outbox — and if the outbox is empty first, pour the entire inbox into it, which reverses the order into FIFO. Each element moves between stacks at most once, so dequeue is amortised O(1).
Breadth-first search on graphs and trees, level-order tree traversal, topological sorting via Kahn's algorithm, sliding-window maximum via a deque, and task scheduling. Anywhere you must process items in the order they were discovered, a queue is the structure.