0
0
DSA Typescriptprogramming~10 mins

Fibonacci Using Recursion in DSA Typescript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Fibonacci Using Recursion
Call fibonacci(n)
Is n <= 1?
YesReturn n
No
Call fibonacci(n-1)
Call fibonacci(n-2)
Add results of fibonacci(n-1) + fibonacci(n-2)
Return sum
End call for fibonacci(n)
The function calls itself with smaller numbers until it reaches 0 or 1, then returns and sums results back up.
Execution Sample
DSA Typescript
function fibonacci(n: number): number {
  if (n <= 1) return n;
  return fibonacci(n - 1) + fibonacci(n - 2);
}

console.log(fibonacci(4));
Calculates the 4th Fibonacci number by recursively summing previous two Fibonacci numbers.
Execution Table
StepCallArgumentsReturn ValueStack DepthAction
1fibonacci4?1Check if 4 <= 1 (No), call fibonacci(3) and fibonacci(2)
2fibonacci3?2Check if 3 <= 1 (No), call fibonacci(2) and fibonacci(1)
3fibonacci2?3Check if 2 <= 1 (No), call fibonacci(1) and fibonacci(0)
4fibonacci1141 <= 1 (Yes), return 1
5fibonacci0040 <= 1 (Yes), return 0
6fibonacci213Return fibonacci(1)+fibonacci(0) = 1+0=1
7fibonacci1131 <= 1 (Yes), return 1
8fibonacci322Return fibonacci(2)+fibonacci(1) = 1+1=2
9fibonacci2?2Check if 2 <= 1 (No), call fibonacci(1) and fibonacci(0)
10fibonacci1131 <= 1 (Yes), return 1
11fibonacci0030 <= 1 (Yes), return 0
12fibonacci212Return fibonacci(1)+fibonacci(0) = 1+0=1
13fibonacci431Return fibonacci(3)+fibonacci(2) = 2+1=3
14----End of recursion, final result is 3
💡 Recursion ends when calls reach n <= 1, returning n directly.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 6After Step 8After Step 12Final
n44322324
Return Value-???1213
Stack Depth01233221
Key Moments - 3 Insights
Why does fibonacci(2) get called multiple times?
Because the function calls fibonacci(n-1) and fibonacci(n-2) separately each time without storing results, as seen in steps 3, 9, and 12 in the execution_table.
Why does the recursion stop at n <= 1?
Because the base case returns n directly when n is 0 or 1, stopping further recursive calls, shown in steps 4, 5, 7, 10, and 11.
What does stack depth represent in the variable_tracker?
It shows how many recursive calls are active at once, increasing when calling deeper and decreasing when returning, as seen increasing from 1 to 4 and then back down.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the return value of fibonacci(3) at step 8?
A2
B1
C3
D0
💡 Hint
Check the 'Return Value' column at step 8 for fibonacci(3).
At which step does the recursion first reach the base case?
AStep 2
BStep 6
CStep 4
DStep 9
💡 Hint
Look for the first step where 'Return Value' is directly n because n <= 1.
If we change the input to fibonacci(5), how would the stack depth change compared to fibonacci(4)?
AMaximum stack depth would stay the same
BMaximum stack depth would increase by 1
CMaximum stack depth would decrease
DStack depth would be zero
💡 Hint
Stack depth increases with input size as recursion goes deeper, see variable_tracker for fibonacci(4).
Concept Snapshot
function fibonacci(n: number): number {
  if (n <= 1) return n;  // base case
  return fibonacci(n - 1) + fibonacci(n - 2);  // recursive calls
}

- Recursion stops when n is 0 or 1
- Each call waits for two smaller calls
- Results sum back up to final answer
Full Transcript
This visualization shows how the recursive Fibonacci function works step-by-step. The function calls itself with smaller numbers until it reaches the base case where n is 0 or 1, then returns that value. Each call waits for the results of fibonacci(n-1) and fibonacci(n-2), adds them, and returns the sum. The execution table tracks each call, its arguments, return values, and stack depth showing how recursion grows and shrinks. The variable tracker shows how n and return values change during execution. Key moments clarify why base cases stop recursion and why some calls repeat. The quiz tests understanding of return values, base cases, and recursion depth. This helps learners see recursion as a chain of calls building up and returning results.