Pattern Studioalgorithmic thinking
All patterns

Queue

Stacks & Queues
Beginner
Interactive

Process work in first-in, first-out order.

Time
O(n)
Space
O(n)
ActiveComparingMatch / bestIn windowFrontierVisitedExcludedAnswer
No frames to display.

The idea

A queue removes the oldest item first. This simulation enqueues a stream of jobs and then serves them in arrival order, making the head and tail behavior visible.

Analogy

A hands-on model of queue that exposes every state change instead of hiding it inside a library call.

When to reach for it

  • Learning the underlying data structure.
  • Tracing state changes and invariants.
  • Building a reusable interview template.

Language-independent template

for item in arrivals: enqueue(item)
while queue: process(dequeue())

Complexity — time O(n), space O(n).

Common mistakes

  • Skipping boundary checks.
  • Updating state in the wrong order.
  • Using the structure without preserving its invariant.

Practice progression

Canonical interview problems, ordered from foundation to advanced application.

Easy

Implement FIFO operations using only stack primitives.

Medium

Design a fixed-capacity queue without using a built-in queue.

Hard

Find the shortest non-empty subarray whose sum reaches K.

Variations

Change the input and replay the trace.
Adapt the operation to a related interview problem.