0
0
DSA Cprogramming~10 mins

Memoization Top Down DP in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Memoization Top Down DP
Start with problem input
Check if result in memo?
YesReturn memoized result
No
Compute result recursively
Store result in memo
Return computed result
The flow starts by checking if the answer is already saved. If yes, return it. If no, compute recursively, save it, then return.
Execution Sample
DSA C
int fib(int n, int memo[]) {
  if (n <= 1) return n;
  if (memo[n] != -1) return memo[n];
  memo[n] = fib(n-1, memo) + fib(n-2, memo);
  return memo[n];
}
This code computes Fibonacci numbers using memoization to avoid repeated calculations.
Execution Table
StepOperationnMemo BeforeRecursive CallsMemo AfterReturned Value
1fib(4) called4[-1,-1,-1,-1,-1]fib(3), fib(2)[-1,-1,-1,-1,-1]pending
2fib(3) called3[-1,-1,-1,-1,-1]fib(2), fib(1)[-1,-1,-1,-1,-1]pending
3fib(2) called2[-1,-1,-1,-1,-1]fib(1), fib(0)[-1,-1,-1,-1,-1]pending
4fib(1) called1[-1,-1,-1,-1,-1]none[-1,-1,-1,-1,-1]1
5fib(0) called0[-1,-1,-1,-1,-1]none[-1,-1,-1,-1,-1]0
6fib(2) memoized2[-1,-1,-1,-1,-1]none[-1,-1,1,-1,-1]1
7fib(1) called again1[-1,-1,1,-1,-1]none[-1,-1,1,-1,-1]1
8fib(3) memoized3[-1,-1,1,-1,-1]none[-1,-1,1,2,-1]2
9fib(2) called again2[-1,-1,1,2,-1]none[-1,-1,1,2,-1]1
10fib(4) memoized4[-1,-1,1,2,-1]none[-1,-1,1,2,3]3
11Return final result4[-1,-1,1,2,3]none[-1,-1,1,2,3]3
💡 fib(4) returns 3 after all recursive calls and memoization complete
Variable Tracker
VariableStartAfter Step 6After Step 8After Step 10Final
n42344
memo[-1,-1,-1,-1,-1][-1,-1,1,-1,-1][-1,-1,1,2,-1][-1,-1,1,2,3][-1,-1,1,2,3]
Returned Valuepending1233
Key Moments - 3 Insights
Why do we check memo[n] before computing fib(n)?
Because if memo[n] is not -1, it means fib(n) was already computed and saved, so we can return it immediately without repeating work. See steps 7 and 9 where memoized values are reused.
What happens when n is 0 or 1?
These are base cases returning n directly without recursion, as shown in steps 4 and 5. This stops infinite recursion.
Why do recursive calls reduce n by 1 and 2?
Because Fibonacci number fib(n) depends on fib(n-1) and fib(n-2). The recursion breaks the problem into smaller subproblems, as seen in steps 1-3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of memo after step 8?
A[-1,-1,1,2,-1]
B[-1,-1,-1,-1,-1]
C[-1,-1,1,-1,-1]
D[-1,-1,1,2,3]
💡 Hint
Check the 'Memo After' column in row with Step 8
At which step does fib(2) get its value memoized for the first time?
AStep 3
BStep 9
CStep 6
DStep 10
💡 Hint
Look for when memo[2] changes from -1 to 1 in the 'Memo After' column
If we remove the memo check, what would happen to the number of recursive calls?
AIt would decrease
BIt would increase drastically
CIt would stay the same
DIt would stop immediately
💡 Hint
Memoization avoids repeated calls; without it, calls multiply exponentially
Concept Snapshot
Memoization Top Down DP:
- Use recursion with a memo array to save results
- Check memo before computing to avoid repeats
- Base cases return directly
- Store computed results in memo
- Return memoized or computed value
Full Transcript
Memoization Top Down DP solves problems by breaking them into smaller parts recursively. Before computing a result, it checks if the answer is already saved in a memo array. If yes, it returns that saved answer immediately. If no, it computes the result recursively, saves it in memo, then returns it. This avoids repeated work and speeds up the program. Base cases stop recursion. For example, Fibonacci numbers use this method by saving fib(n) once computed. The execution table shows each call, memo state before and after, and returned values step by step.