0
0
DSA Typescriptprogramming~10 mins

Factorial Using Recursion in DSA Typescript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Factorial Using Recursion
Call factorial(n)
Is n <= 1?
YesReturn 1
No
Call factorial(n-1)
Multiply n * factorial(n-1)
Return result
The function calls itself with n-1 until it reaches 1, then returns 1 and multiplies back up.
Execution Sample
DSA Typescript
function factorial(n: number): number {
  if (n <= 1) return 1;
  return n * factorial(n - 1);
}
Calculates factorial of n by calling itself with n-1 until base case.
Execution Table
StepCallnCondition n<=1?ActionReturn ValueStack Depth
1factorial(4)4NoCall factorial(3)1
2factorial(3)3NoCall factorial(2)2
3factorial(2)2NoCall factorial(1)3
4factorial(1)1YesReturn 114
5factorial(2)2NoReturn 2 * 123
6factorial(3)3NoReturn 3 * 262
7factorial(4)4NoReturn 4 * 6241
8EndAll calls returned0
💡 Recursion ends when n <= 1, then results multiply back up the call stack.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6After Step 7Final
n443212344
Return Value1262424
Stack Depth012343210
Key Moments - 3 Insights
Why does factorial(1) return 1 instead of calling factorial(0)?
Because the base case is n <= 1 (see Step 4 in execution_table), it stops recursion to avoid infinite calls.
How does the multiplication happen after the base case returns?
After factorial(1) returns 1 (Step 4), each previous call multiplies n by the returned value (Steps 5-7).
What does stack depth represent in recursion?
Stack depth shows how many calls are waiting to complete; it increases with each call and decreases as calls return (see variable_tracker).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the return value of factorial(2) at Step 5?
A2
B1
C6
D24
💡 Hint
Check the 'Return Value' column at Step 5 in execution_table.
At which step does the recursion reach its base case?
AStep 3
BStep 4
CStep 5
DStep 7
💡 Hint
Look for the step where 'Condition n<=1?' is Yes in execution_table.
If the input n was 3 instead of 4, what would be the final return value?
A3
B24
C6
D1
💡 Hint
Check the pattern of multiplication in execution_table and variable_tracker for n=4 and adjust for n=3.
Concept Snapshot
Factorial Using Recursion:
- Base case: if n <= 1, return 1
- Recursive case: return n * factorial(n-1)
- Calls stack up until base case, then multiply back
- Stack depth shows call layers
- Stops when base case reached
Full Transcript
This visualization shows how factorial is calculated using recursion. The function calls itself with n-1 until it reaches the base case where n is 1 or less. At that point, it returns 1. Then each previous call multiplies its n by the returned value from the deeper call. The execution table tracks each call, the condition check, and the return values as the recursion unwinds. The variable tracker shows how n, return values, and stack depth change step by step. Key moments clarify why the base case stops recursion and how multiplication happens after. The quiz tests understanding of return values, base case step, and effect of changing input.