Esc
  • Loading…
↑ ↓ to moveEnter to openEsc to close

Queue

Learn the first-in, first-out structure behind printers, servers and breadth-first search — then build queues from arrays, linked lists and stacks, the way interviews ask.

  • 4 Lessons
  • 5 Problems
  • 38 Illustrations

Dry-run engine

Circular Buffer Queue

A queue doesn't shift elements forward — two indices chase each other around a fixed ring of memory.

O(1) Enqueue / Dequeue
Input · Operations
enq ×3 · deq ×2 · enq ×3
Output · Queue (front → back)
Press play to run it
How it works

Enqueue writes at tail, dequeue reads at head; both advance with (i + 1) % 5, so slots freed at the front are reused instead of shifting elements.

ring_buffer[5]

  • head0 (empty)
  • tail0 (next write)
  • Size0 / 5
  • Last op—

The queue is a fixed ring of 5 slots plus two indices: head (next read) and tail (next write). Both start at 0.

1 / 9

What you will learn

  • What a queue is: a line where the first to arrive is the first served
  • The four kinds — simple, circular, deque and priority queue — and when each one fits
  • When a problem wants a queue: arrival order, breadth-first search, time windows and round robin
  • How to use the built-in queue in JavaScript, Python, Java and C++, and the traps in each
  • How to build a queue on a circular array and on a linked list
  • Stacks from queues and queues from stacks, including what “amortized O(1)” really means
  • Brute force, optimal and best solutions for 5 interview questions, each with a dry-run simulator

Why this course

A queue is a line: new items join at the back, and items leave from the front, in exactly the order they arrived. Anything that has to be handled fairly, in order — requests to a server, jobs for a printer, nodes waiting to be explored — sits in a queue.

The introduction is short but complete: what a queue is, the four kinds you'll meet, when to reach for one and how to use it in four languages. Every idea is drawn, and every lesson has a simulator you can step through.

Then come 5 interview questions in two groups — building a queue (on an array, on a linked list) and one structure from another (stacks from queues, a queue from stacks) — ordered from easiest to hardest. Every problem goes from brute force to optimal to the best solution, with code in four languages and its common variations.

Requirements

  • Comfort writing loops, conditionals and functions in at least one language
  • The Stack and Linked List courses help: the problems build queues on top of both

Course Contents