Interactive Lab · Python Foundations
Recursion sounds like a snake eating its tail. But each call works on a smaller input, piling a new frame onto the call stackuntil it hits a base case that stops the descent. Then the stack unwinds, returning values back up to whoever asked. Watch the pile grow and collapse, then flip on memoization to see it turn exponential work linear.
🎬 Watch the 45-second explainer, then try it yourself below ↓
▶ The playground, step through the calls, watch the stack grow & unwind
The pile is the CALL STACK. A new call pushes a frame on top; a return pops it and flows its value back to the caller below.
Recursion + memoization is the shape of half of computing: file-system walkers, JSON parsers, compilers (recursive descent), React's component tree rendering, and memoization IS dynamic programming, functools.lru_cache, and React's useMemo. The exponential-to-linear collapse you just watched is among the most-asked interview transformations.
The code that's actually running, the highlighted line is executing now
Switch to fibonacci, set n = 6, leave memoize off and hit Run. Predict first: how many total calls? (Naive fib(6) makes 25.) Notice the stack keeps re-descending to recompute fib(2), fib(3)… over and over. Now turn memoize on and Run again, total calls collapses. Why does caching turn an exponential number of calls into a linear one? (Hint: with a cache, each distinct fib(k) is computed once; every later request for it is a lookup, so you do work proportional to how many different k's existjust n of them, instead of the whole doubling tree.)
The takeaway