Queues and Deques

Lesson 12 of 20

Queue jobs FIFO-style with deque, then watch a circular queue wrap around and a monotonic deque own the sliding window.

The big picture

Meet the queue

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.

FIFO rule

First In, First Out. Enqueue at the rear, serve from the front — no queue-jumping.

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).

Circular queue

Fixed slots plus rear = (rear + 1) % capacity — freed slots get reused, nothing ever shifts.

Monotonic deque

Keep candidates decreasing and every sliding-window maximum falls out in O(1) amortized per step.

Where you meet it

Printer spoolers, ticket desks, BFS frontiers and rate limiters are all queues doing their thing.