0
0
DSA Typescriptprogramming~10 mins

Recursion Base Case and Recursive Case in DSA Typescript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Recursion Base Case and Recursive Case
Start: Call function
Check base case condition
Return base value
Return
Return result
The function checks if it reached the simplest case (base case). If yes, it returns a value. Otherwise, it calls itself with a smaller input and processes the result.
Execution Sample
DSA Typescript
function factorial(n: number): number {
  if (n === 0) return 1;
  return n * factorial(n - 1);
}
Calculates factorial of n using recursion with a base case at n=0.
Execution Table
StepOperationInput nCondition (n === 0)?ActionReturn ValueCall Stack Depth
1Call factorial(3)3NoCall factorial(2)1
2Call factorial(2)2NoCall factorial(1)2
3Call factorial(1)1NoCall factorial(0)3
4Call factorial(0)0YesReturn 114
5Return from factorial(1)1NoCalculate 1 * 113
6Return from factorial(2)2NoCalculate 2 * 122
7Return from factorial(3)3NoCalculate 3 * 261
8Final return360
💡 Recursion ends when n reaches 0, the base case, returning 1 and unwinding the calls.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6After Step 7Final
n332101233
Return Value11266
Call Stack Depth012343210
Key Moments - 3 Insights
Why does the recursion stop when n equals 0?
Because the base case condition (n === 0) is true at step 4 in the execution_table, the function returns 1 without further recursive calls, stopping the recursion.
What happens after the base case returns a value?
After the base case returns 1 at step 4, each previous call resumes and calculates its return value by multiplying n with the returned value, as shown in steps 5 to 7.
Why is the call stack depth important?
The call stack depth shows how many recursive calls are active. It grows until the base case (step 4) and then shrinks as calls return, visible in the variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the return value of factorial(0)?
A0
B1
CUndefined
Dfactorial(1)
💡 Hint
Check the 'Return Value' column at step 4 in the execution_table.
At which step does the recursion start to return back up the call stack?
AStep 3
BStep 4
CStep 5
DStep 7
💡 Hint
Look for the first step where a return value is calculated after the base case returns.
If the base case was changed to 'if (n === 1) return 1;', how would the call stack depth at step 4 change?
AIt would be shallower
BIt would be deeper
CIt would be the same
DRecursion would never stop
💡 Hint
Changing the base case to n === 1 means recursion stops earlier, reducing call stack depth.
Concept Snapshot
Recursion has two parts:
- Base case: stops recursion (e.g., n === 0)
- Recursive case: calls itself with smaller input
Always ensure base case is reachable to avoid infinite calls.
Return values unwind back through calls.
Full Transcript
Recursion works by calling the same function inside itself with a smaller input until it reaches a base case. The base case stops further calls and returns a value. Then, each previous call uses that value to compute its own return value. This process is shown step-by-step in the execution table where factorial(3) calls factorial(2), then factorial(1), then factorial(0). At factorial(0), the base case returns 1. Then factorial(1) returns 1*1=1, factorial(2) returns 2*1=2, and factorial(3) returns 3*2=6. The call stack depth grows as calls go deeper and shrinks as they return. Understanding base and recursive cases is key to writing correct recursive functions.