Imagine you need to find something in a large environment where you cannot see everything at once. From your current position, there may be several different paths leading to new and unexplored places.
As you move forward, each new place may reveal more possibilities. Without a clear approach, it can become difficult to keep track of where you have already been and which path you should explore next.
Let's play a game where you'll explore a set of connected directories to find a missing file. Follow each path carefully and keep track of the folders you have already explored.
Let's see what this looks like through a story 👇
💡 Click any folder to open it and explore by hand. Or click Auto at any time to sit back and watch the search play out on its own. Use Pause whenever you want to take a break.
Sit with these for a moment, there's no single correct wording, just think them through in your own words.
Here's the strategy you were using the whole time, with a name and a shape.
That's exactly the habit you built by hand while hunting for Homework.pdf:
That strategy has a name: Depth-First Search. It always commits to one path fully before backtracking to try another.
DFS remembers where to go next using a stack, a pile where you can only add or remove from the top. Every time you open a folder, it gets placed on top of the stack. When you hit a dead end, you pop the top folder off and land back on the one below it, exactly the folder you'd expect to return to.
Try it yourself: each push opens the next folder on the real path to Homework.pdf, then pop it to back out, exactly like the search you just ran.
Go as deep as possible down one path before trying another, commit fully, and only backtrack once you truly hit a dead end.
The last folder you opened is the first one you return to. That "last in, first out" order is exactly what a stack gives you.
Backing out exactly one step (instead of starting over) means you never skip a folder and never waste time re-checking one.
File explorers, maze-solving, puzzle games, web crawlers, and countless programming problems all lean on this exact idea.
New situations to think through using what you just discovered.