Stacks: The Secret Behind Cmd+Z and Stack Overflow
The second way of organizing looks like a stack of plates: you can only put on from the top, and only take from the top — that's a stack. Sounds very limiting? But the undo key you hit every day, and every function call in a program, both rely on it. This lesson starts by letting you wreck a document and undo it in one tap, then watch a “stack overflow” accident — the world's biggest programmer Q&A site, Stack Overflow, got its name from that accident.
On the left is a mini document — mess with it using the four buttons. Watch the right: every step you take is pushed as a card onto the “operation stack” — later steps sit on top. When you've had enough, mash “⌘Z Undo” a few times and see the order it undoes.
📄 Mini document
Mess around — don't hold back
🥞 Operation stack
Each step pushes a card; top = most recent
You ask AI to “make dinner.” Halfway through it needs to chop veggies; halfway through chopping the knife is dull and it needs to sharpen… Every time it “pauses what's in hand to do something else,” the program pushes the current progress onto a stack. Watch the animation below (it auto-plays once when you scroll here), and notice how, after each task finishes, the program automatically finds its way back to the previous one.
A stack has limited capacity. The function below calls itself (that's recursion). Code on the left, call stack on the right. First run without checking the base case and see what happens; then check it and run again to compare.
✅ What this lesson wants to share
- Stack = a pile of plates: put and take only from the top — last in, first out (LIFO)
- The undo key's real form: each step pushes a card; Cmd+Z always pops the most recent one first
- Function calls use a stack to remember the path: pop the top, and you're automatically back where the level above paused
- Stack overflow = push without pop: recursion with no base case fills the stack; the program crashes on the spot
- An Agent's fuse: a max-step limit — guarding against “infinite recursion” on the task stack