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.
- 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.
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
2. Building a queue
3. One structure from another
Related courses
- StackLast-in, first-out — the structure behind call stacks, undo history, and expression parsing.6 lessons · 13 problems
- Binary TreeHierarchical structures for representing nested relationships and enabling fast search.5 lessons · 16 problems
- Linked ListNodes linked one direction by pointers instead of contiguous memory.4 lessons · 12 problems
