Problem. Implement a FIFO queue with push(x), pop(), peek() and empty(), using only stack operations:
push to the top, pop from the top, look at the top, size, and is-empty.
push(1), push(2), peek() → 1, pop() → 1, empty() → False, pop() → 2, empty() → True
The point isn't that you'd do this in Python (you'd use deque). It's the cleanest example of amortised cost:
an operation that is occasionally expensive but cheap on average, and of how reversing twice restores order.
ML context. Queues sit between every producer and consumer in an ML system: a data loader filling batches for the GPU, a model server holding requests, a replay buffer of recent experience. Their two engineering questions are the ones this lesson ends on: what does the worst single operation cost (a latency spike) and what happens when the buffer is full (Changes A, B and D).