0
0
DSA Typescriptprogramming~10 mins

Memoization to Optimize Recursion in DSA Typescript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Memoization to Optimize Recursion
Start Recursive Call
Check if result in cache?
YesReturn cached result
No
Compute result recursively
Store result in cache
Return result
End Recursive Call
The flow shows how recursion checks a cache before computing, stores results to avoid repeated work, and returns cached results when available.
Execution Sample
DSA Typescript
function fib(n: number, memo: Record<number, number> = {}): number {
  if (n <= 1) return n;
  if (memo[n] !== undefined) return memo[n];
  memo[n] = fib(n - 1, memo) + fib(n - 2, memo);
  return memo[n];
}
Calculates Fibonacci numbers using recursion with memoization to save and reuse results.
Execution Table
StepOperationCurrent nCache BeforeCache AfterReturned ValueVisual State
1Call fib(4)4{}{}pendingfib(4) called, cache empty
2Call fib(3)3{}{}pendingfib(3) called inside fib(4)
3Call fib(2)2{}{}pendingfib(2) called inside fib(3)
4Call fib(1)1{}{}1fib(1) base case returns 1
5Call fib(0)0{}{}0fib(0) base case returns 0
6Compute fib(2)2{}{"2": 1}1fib(2) = 1 + 0 = 1 stored in cache
7Call fib(1)1{"2":1}{"2":1}1fib(1) base case returns 1
8Compute fib(3)3{"2":1}{"2":1,"3":2}2fib(3) = 1 + 1 = 2 stored in cache
9Call fib(2)2{"2":1,"3":2}{"2":1,"3":2}1fib(2) result found in cache
10Compute fib(4)4{"2":1,"3":2}{"2":1,"3":2,"4":3}3fib(4) = 2 + 1 = 3 stored in cache
11Return fib(4)4{"2":1,"3":2,"4":3}{"2":1,"3":2,"4":3}3Final result returned
💡 All recursive calls resolved, final fib(4) value computed and cached.
Variable Tracker
VariableStartAfter Step 6After Step 8After Step 10Final
n42344
memo{}{"2":1}{"2":1,"3":2}{"2":1,"3":2,"4":3}{"2":1,"3":2,"4":3}
Returned Valuepending1233
Key Moments - 3 Insights
Why does the function check the cache before computing?
Because as shown in steps 9 and 10, the function returns cached results immediately to avoid repeating calculations, speeding up execution.
What happens when the base case is reached?
At steps 4 and 5, the function returns the base values (0 or 1) without recursion, stopping further calls and starting to build results back up.
How does memoization reduce the number of recursive calls?
By storing results in the cache (memo), repeated calls like fib(2) at step 9 return instantly from cache instead of recalculating, reducing total calls.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the cache content after step 8?
A{"2":1,"3":2}
B{"2":1}
C{"3":2}
D{}
💡 Hint
Check the 'Cache After' column at step 8 in the execution table.
At which step does fib(2) return a cached value instead of computing?
AStep 4
BStep 6
CStep 9
DStep 10
💡 Hint
Look for when fib(2) is called and cache already has its value.
If memoization was removed, what would happen to the number of calls?
ACalls would stay the same
BCalls would increase due to repeated calculations
CCalls would decrease
DCalls would stop immediately
💡 Hint
Refer to how memoization avoids repeated calls in the execution table.
Concept Snapshot
Memoization stores results of recursive calls in a cache.
Before computing, it checks if result exists to avoid repeated work.
This optimizes recursion by reducing calls and speeding up execution.
Use a dictionary or object to save and retrieve results.
Base cases return directly without caching.
Memoization is key for efficient recursive algorithms.
Full Transcript
This visualization shows how memoization optimizes recursion by storing results in a cache object. When the recursive function fib is called with a number n, it first checks if the result is already in the cache. If yes, it returns that cached value immediately, avoiding repeated calculations. If not, it computes the value recursively, stores it in the cache, and then returns it. Base cases for fib(0) and fib(1) return immediately without recursion. The execution table tracks each step, showing calls, cache state before and after, and returned values. The variable tracker shows how the cache (memo) grows and how returned values build up. Key moments clarify why caching is checked first, how base cases stop recursion, and how memoization reduces calls. The quiz tests understanding of cache contents, when cached values are used, and the effect of removing memoization. The snapshot summarizes memoization as a technique to speed up recursion by saving and reusing results.