Stacks & Queues

Difficulty
Importance

The problem

Many processes need to track pending work in a specific order — undo the most recent action first, or process requests in the order they arrived — and need a structure that enforces that order automatically instead of relying on discipline.

Why now

Arrays and lists give general-purpose storage, but say nothing about *which end* you're allowed to touch. Stacks and queues are arrays/lists with a deliberately restricted interface — and that restriction is what gives them predictable, provable ordering guarantees.

Mental model

A stack is a pile of plates: you can only add or remove from the top (LIFO). A queue is a line at a store: you add at the back, remove from the front (FIFO). The restriction isn't a limitation — it's the entire point, because it's what makes 'undo' and 'process in arrival order' automatic instead of something you have to enforce by hand.

Requires

Unlocks

Used in

Projects

  • Implement a stack and a queue from scratch using only an array, including full/empty edge cases
  • Use a stack to check whether a string of brackets ((), [], {}) is balanced

Examples

  • The browser back button is a stack of visited pages — most recent first
  • A print queue processes jobs in the order they were submitted — first in, first out

Resources

Mastery checklist