Queue: definition

A queue stores items in arrival order: first in, first out (FIFO). Add at the back, read or remove from the front.

Watch the idea in action

Imagine a ticket counter. A arrives, then B, then C. Nobody jumps ahead: A is served first. New arrivals join the back while the front keeps moving.

The key idea
The front is always the oldest waiting item. Use a queue when work should be processed in the order it arrives.

Problems this pattern can solve

Process waiting jobs, count recent requests, simulate a ticket line, or explore a tree level by level with BFS.

Task example: Implement Queue Using Stacks

Build MyQueue using two stacks. push(x) adds x at the back; pop() removes and returns the front; peek() returns the front without removing it; empty() returns whether the queue is empty. Use only stack operations: push to the top, pop or peek at the top, size, and checking for emptiness.

Example 1
Input
push(1), push(2), peek(), pop(),
push(3), peek(), pop(), pop(), empty()
Output
[None, None, 1, 1, None, 2, 2, 3, True]

The values leave in arrival order: 1, 2, 3. peek reads a value but leaves it in the queue.

Example 2
Input
empty(), push(4), peek(), peek(), pop(), empty()
Output
[True, None, 4, 4, 4, True]

Calls start with a new queue. Push values are integers from 1 to 9. pop and peek are called only on a nonempty queue. None marks an operation without a return value.

LeetCode 232 ↗
01 / Baseline: costly push

Costly push: video explanation

push(1), push(2), peek(), pop(),
push(3), peek(), pop(), pop(), empty()
[None, None, 1, 1, None, 2, 2, 3, True]

Prefer reading? Follow the steps below.

  1. Keep the oldest value at the top of main. Before pushing a new value, move every old value from main to temp.
  2. Push the new value onto main, then move every value back from temp. The new arrival ends up beneath all older values.
  3. For pop, remove and return the top of main. For peek, read its top. empty checks whether main has any values.

Time complexity · push O(n); others O(1)

Each push moves the n stored values out and back. pop, peek, and empty use only the top or an emptiness check.

Space complexity · O(n)

The two stacks together store n queue values. Moving a value does not create another queue entry.

PYTHONLive code trace
02 / Efficient: lazy transfer

Lazy transfer: video explanation

push(1), push(2), peek(), pop(),
push(3), peek(), pop(), pop(), empty()
[None, None, 1, 1, None, 2, 2, 3, True]

Prefer reading? Follow the steps below.

  1. Push new values onto inbox. Read and remove the front from outbox.
  2. When pop or peek finds outbox empty, move all inbox values into outbox, one top value at a time. Reversing their order puts the oldest value on top.
  3. If outbox still has values, leave it alone. New inbox values must wait behind those older values. empty returns True only when both stacks are empty.

Unlike rearranging on every push, lazy transfer never moves a value back to inbox. That avoids repeated work while preserving FIFO order.

Time complexity · O(1) amortized per operation

Each value enters inbox once, moves to outbox at most once, and leaves once. So m operations take O(m) total time. One pop or peek can still take O(n) when it triggers a transfer; amortized means averaged over a sequence.

Space complexity · O(n)

The two stacks together store n queue values. Moving a value does not create another queue entry.

PYTHONLive code trace

Queue · The task

Close or press Esc to return to your place. The task button stays pinned while you explore the solutions.