Lesson 7 / 42

Queue

First-In-First-Out: enqueue at the back, dequeue at the front.

FIFO

The oldest element leaves first. Use a ring buffer or a doubly linked list so both ends are O(1). A plain array with pop(0) is O(n) — use collections.deque.

Checkout line

You join at the back; whoever has waited longest is served next. No cutting in.

Deque as a queue

deque gives O(1) at both ends and doubles as a stack or a double-ended queue.

from collections import deque
q = deque()
q.append(1); q.append(2); q.append(3)
q.popleft()   # -> 1  (FIFO)
q[0]          # peek front -> 2

Where it hides

BFS traversal, task/print scheduling, request buffering, and producer–consumer pipelines. A deque also powers sliding-window-maximum.