A queue is fairness as a data structure: the earliest arrival is always served first. Python’s deque makes both ends of the line fast.
1 FIFO rule
First In, First Out. Enqueue at the rear, serve from the front — no queue-jumping.
2 deque does it all
collections.deque appends and pops at both ends in O(1). A plain list’s pop(0) shifts every element: O(n).
3 Circular queue
Fixed slots plus rear = (rear + 1) % capacity — freed slots get reused, nothing ever shifts.
4 Monotonic deque
Keep candidates decreasing and every sliding-window maximum falls out in O(1) amortized per step.
5 Where you meet it
Printer spoolers, ticket desks, BFS frontiers and rate limiters are all queues doing their thing.
Meet the queue
FIFO rule1First In, First Out. Enqueue at the rear, serve from the front — no queue-jumping. deque does it all2collections.deque appends and pops at both ends in O(1). A plain list’s pop(0) shifts every element: O(n). Circular queue3Fixed slots plus rear = (rear + 1) % capacity — freed slots get reused, nothing ever shifts. Monotonic deque4Keep candidates decreasing and every sliding-window maximum falls out in O(1) amortized per step. Where you meet it5Printer spoolers, ticket desks, BFS frontiers and rate limiters are all queues doing their thing. 5 building blocks make up Meet the queue
Print jobs arrive at the rear and leave from the front — order is never renegotiated.
1 / 10Three print jobs wait in line. front = J1 (next to serve), rear = J3 (last to arrive).
One ring of slots, two pointers, one modulo — enqueue and dequeue in a circular queue.
process decision input / output start / end FIFO service with deque first — then the sliding-window maximum that a monotonic deque owns.
maindeque comes from collections — O(1) operations on both ends, unlike a plain list.
Memory
no variables yet — press play
Console
output appears as the program runs…
Five fixed slots, one modulo: freed cells are reused as rear wraps past the end.
1 / 13A circular queue of 5 slots holds A–D. front = 0, rear = 3 — the next enqueue lands at slot 4.
Both ends of a deque at work — trace every append and pop.
Your predicted output — one line per console line
Hint: append joins the rear, popleft serves the front, appendleft cuts in at the front. Count and list at the end.
Click a term, then click its meaning to lock the pair.
8 conceptsTap a card to expand ?FIFOdrops into place
?append(x)drops into place
?popleft()drops into place
?frontdrops into place
?(rear + 1) % capdrops into place
?dequedrops into place
?monotonic dequedrops into place
?BFSdrops into place
01A queue serves the front: deque append() to join, popleft() to serve — both O(1).
02list.pop(0) works but shifts every element — O(n). Use collections.deque instead.
03rear = (rear + 1) % capacity is the whole trick behind circular queues.
04Deques work both ends: appendleft and pop make undo buffers and palindrome checks easy.
05The monotonic deque answers every sliding-window maximum in O(n) total.
06BFS explores in rings of increasing distance — it is a queue wearing an algorithm costume.