Step 1: Call fib(5), cache empty
cache = {}
Call stack: fib(5) ↑Why: Start computing fib(5), no cached answers yet
Step 2: Call fib(4) from fib(5), cache still empty
cache = {}
Call stack: fib(5) -> fib(4) ↑Why: Need fib(4) to compute fib(5)
Step 3: Call fib(3) from fib(4), cache still empty
cache = {}
Call stack: fib(5) -> fib(4) -> fib(3) ↑Why: Need fib(3) to compute fib(4)
Step 4: Call fib(2) from fib(3), cache still empty
cache = {}
Call stack: fib(5) -> fib(4) -> fib(3) -> fib(2) ↑Why: Need fib(2) to compute fib(3)
Step 5: fib(2) computes fib(1) + fib(0) = 1 + 0 = 1, store in cache
cache = {2:1}
Call stack: fib(5) -> fib(4) -> fib(3) ↑Why: fib(2) computed using base cases fib(1)=1 and fib(0)=0, save result
Step 6: Call fib(1) from fib(3), cache unchanged
cache = {2:1}
Call stack: fib(5) -> fib(4) -> fib(3) -> fib(1) ↑Why: Need fib(1) to compute fib(3)
Step 7: fib(1) base case returns 1
cache = {2:1}
Call stack: fib(5) -> fib(4) -> fib(3) ↑Step 8: Compute fib(3) = fib(2)+fib(1) = 1+1=2, store in cache
cache = {2:1, 3:2}
Call stack: fib(5) -> fib(4) ↑Why: fib(3) computed from cached fib(2) and fib(1)
Step 9: Call fib(2) from fib(4), found in cache
cache = {2:1, 3:2}
Call stack: fib(5) -> fib(4) ↑Why: Reuse cached fib(2) to avoid recomputation
Step 10: Compute fib(4) = fib(3)+fib(2) = 2+1=3, store in cache
cache = {2:1, 3:2, 4:3}
Call stack: fib(5) ↑Why: fib(4) computed from cached fib(3) and fib(2)
Step 11: Call fib(3) from fib(5), found in cache
cache = {2:1, 3:2, 4:3}
Call stack: fib(5) ↑Why: Reuse cached fib(3) to avoid recomputation
Step 12: Compute fib(5) = fib(4)+fib(3) = 3+2=5, store in cache
cache = {2:1, 3:2, 4:3, 5:5}
Call stack: emptyWhy: fib(5) computed from cached fib(4) and fib(3)
Result: cache = {2:1, 3:2, 4:3, 5:5}
Answer: fib(5) = 5