# Queue — Data Structures and Algorithms: Patterns for Interviews

Source: https://www.skillbyai.com/en/dsa/queue

> Use a first-in-first-out queue with enqueue and dequeue, and apply it to BFS, scheduling and buffering.

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

```python
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.
