Programming Fundamentals · Data Structures Behind AI

Stacks: The Secret Behind Cmd+Z and Stack Overflow

Last in, first out: undo, function calls, and an Agent's subtasks all rely on it. Push and pop yourself, then watch how recursion without a base case overflows the stack

THE QUESTION THIS PAGE ANSWERS

ANSWER FIRST

What is the key idea behind “Stacks: The Secret Behind Cmd+Z and Stack Overflow”?

Last in, first out: undo, function calls, and an Agent's subtasks all rely on it. Push and pop yourself, then watch how recursion without a base case overflows the stack

DECISION RULE

Make the claim earn its place. Use this page as a decision aid, not a definition to memorize. Connect the idea to one real task, one observable result, and one failure that would change your mind.

TRY NEXT

Write one question you could answer with evidence after trying this idea.

WATCH FOR

A conclusion that sounds complete but leaves the key assumption untested.

Play first · the undo key's real form

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

(no operations yet)
Try: type two lines → mark red → undo three times, and guess the restore order
See it? Undo always undoes the most recent step first. That isn't a product manager's choice — it's the nature of a stack: last in, first out (LIFO, Last In First Out). You only take from the top, so the first thing you get is whatever was put on last. If undo didn't follow that order — if it undid something from ten steps ago first — the document would instantly turn into mush. Anything that “backs out the way it came in” is born to be organized with a stack.
How does a program remember “where to return”?

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.

ReadyHit “Play” or scroll here to watch a full round of calls and returns
This stack is the function call stack; each card is a “stack frame,” holding “how far this task got, and where to return when done.” The program needs no global dispatcher — pop the top, and you're naturally back at the paused spot one level up. The Agents you've learned work the same way: halfway through a main task it needs to look something up first, so it pushes the main task, runs the subtask, pops when done, and continues the main task.
Stack overflow live · how Stack Overflow happens

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.

function 倒数(n) { if (n === 0) return; // 终止条件(保险丝) 打印(n); 倒数(n - 1); // 自己调用自己 } 倒数(5);
💥 Stack OverflowStack overflow — the program crashes
Run it as-is first — see what recursion without a fuse looks like
What does this have to do with AI? A lot. Agents also “push the main task, run the subtask first.” What if the subtask spawns more subtasks, layer after layer with no end? Same as recursion with no base case — so every serious Agent framework sets a max steps / max depth and stops hard when it's exceeded. That fuse is insurance against “infinite recursion”. Next time you see recursive code AI wrote, check-list item one: where's the base case?

Why “Play first · the undo key's real form” depends on the operation

“On the left is a mini document — mess with it using the four buttons.” makes the structure concrete. The useful comparison is not which name sounds more advanced, but how the data is arranged and how far the most common operation has to travel.

Read a structure through access and change

“Each step pushes a card;” exposes a trade-off that is easy to miss: reading by position, looking up by key, adding at either end, inserting in the middle, and traversing relationships do not favor the same organization. A structure that is fast for one operation is not automatically fast for all of them.

  • 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

Count scale and update frequency together

Use “A stack has limited capacity.” as a boundary check. Write down the data size, the dominant operation, and the latency you can accept before deciding whether an AI-generated structure actually fits.

From “Play first · the undo key's real form” to “How does a program remember “where to return””

“Play first · the undo key's real form” grounds the problem in “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…”. “How does a program remember “where to return”” then moves it toward “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…”. Together, they show that the lesson is not just a conclusion to remember, but a claim with conditions.

Carry the judgment into the next situation

When you meet a new data structure, do not begin by memorizing its definition. Write down the most frequent operation, estimate scale and update behavior, and check whether the structure satisfies all three conditions.

  • “Play first · the undo key's real form”: 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…
  • “How does a program remember “where to return””: 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…
  • “The closing point”: An Agent's fuse : a max-step limit — guarding against “infinite recursion” on the task stack

The final “The closing point” brings the discussion to “An Agent's fuse : a max-step limit — guarding against “infinite recursion” on the task stack”. The useful thing to carry forward is knowing which judgments must be revisited when input, scale, or risk changes.

✅ 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
Mark as learned Your reading progress updates automatically
← PreviousNext →

Keep reading

The next useful article in the thread.

ARTICLE DISCUSSION

Leave one useful thought here.

Keep the idea that clicked, the question that stayed open, or a small note for the next learner.

Discussing Stacks: The Secret Behind Cmd+Z and Stack Overflow Data Structures Behind AI
3discussionsArticle discussion · synced with the Circle
View in the learning circle
AM
Asha MorganContent editor
INSIGHTField note

I turned one judgment from this article into a small experiment I could run today. Knowing what to observe next is more useful than simply remembering the conclusion.

ARTICLE DISCUSSION7 helpful
LH
Lin HarperIndie developer
INSIGHTInsight

After reading this, I first looked for the conditions behind the idea instead of copying the method into a project. That order made the later trade-offs much clearer.

ARTICLE DISCUSSION5 helpful
KM
Kiki MooreProduct operations
QUESTIONQuestion

When this judgment reaches real work, which constraint should be added first? I am curious which step matters most between reading and the first practical attempt.

ARTICLE DISCUSSION4 helpful