Bird
Raised Fist0
DSA Typescriptprogramming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the purpose of the base case in a recursive function?
easy
A. To call the function again with a smaller input
B. To stop the recursion and prevent infinite calls
C. To increase the input size for the next call
D. To print the result of the recursion

Solution

  1. Step 1: Understand what recursion does

    Recursion calls the same function repeatedly with smaller inputs.
  2. Step 2: Identify the role of the base case

    The base case stops the recursion by providing a condition where the function returns without calling itself again.
  3. Final Answer:

    To stop the recursion and prevent infinite calls -> Option B
  4. Quick Check:

    Base case = stop recursion [OK]
Hint: Base case stops recursion to avoid infinite loops [OK]
Common Mistakes:
  • Thinking base case calls recursion again
  • Confusing base case with recursive case
  • Ignoring base case leads to infinite recursion
2. Which of the following is the correct syntax for a recursive function in TypeScript that calculates factorial?
easy
A. function factorial(n: number): number { if (n === 0) return 1; else return n * factorial(n - 1); }
B. function factorial(n: number): number { while (n > 0) return n * factorial(n - 1); }
C. function factorial(n: number): number { if (n === 1) return 0; else return n + factorial(n - 1); }
D. function factorial(n: number): number { return n * factorial(n + 1); }

Solution

  1. Step 1: Check base case correctness

    function factorial(n: number): number { if (n === 0) return 1; else return n * factorial(n - 1); } uses if (n === 0) return 1; which is correct for factorial base case.
  2. Step 2: Check recursive call correctness

    function factorial(n: number): number { if (n === 0) return 1; else return n * factorial(n - 1); } calls factorial(n - 1) and multiplies by n, which matches factorial definition.
  3. Final Answer:

    function factorial(n: number): number { if (n === 0) return 1; else return n * factorial(n - 1); } -> Option A
  4. Quick Check:

    Correct base and recursive case syntax = function factorial(n: number): number { if (n === 0) return 1; else return n * factorial(n - 1); } [OK]
Hint: Base case returns 1 for factorial, recursive calls with n-1 [OK]
Common Mistakes:
  • Using while loop instead of recursion
  • Wrong base case value
  • Recursive call increasing n causing infinite recursion
3. What will be the output of this TypeScript code?
function sumTo(n: number): number {
  if (n === 0) return 0;
  return n + sumTo(n - 1);
}
console.log(sumTo(3));
medium
A. 6
B. 3
C. 0
D. Error

Solution

  1. Step 1: Trace recursive calls

    sumTo(3) calls 3 + sumTo(2), sumTo(2) calls 2 + sumTo(1), sumTo(1) calls 1 + sumTo(0), sumTo(0) returns 0.
  2. Step 2: Calculate return values

    sumTo(1) = 1 + 0 = 1, sumTo(2) = 2 + 1 = 3, sumTo(3) = 3 + 3 = 6.
  3. Final Answer:

    6 -> Option A
  4. Quick Check:

    Sum 1+2+3 = 6 [OK]
Hint: Add n plus sumTo(n-1) until base case 0 [OK]
Common Mistakes:
  • Confusing base case return value
  • Adding instead of multiplying
  • Missing base case causing infinite recursion
4. Identify the error in this recursive function and select the fix.
function countdown(n: number): void {
  if (n === 0) return;
  console.log(n);
  countdown(n + 1);
}
countdown(3);
medium
A. Change console.log(n) to console.log(n - 1)
B. Add a base case for n === 1
C. Remove the base case to allow infinite recursion
D. Change countdown(n + 1) to countdown(n - 1)

Solution

  1. Step 1: Analyze recursive call direction

    The function calls countdown(n + 1), which increases n, causing infinite recursion.
  2. Step 2: Fix recursive call to decrease n

    Changing to countdown(n - 1) moves towards base case n === 0, stopping recursion.
  3. Final Answer:

    Change countdown(n + 1) to countdown(n - 1) -> Option D
  4. Quick Check:

    Recursive call must approach base case [OK]
Hint: Recursive calls must reduce input to reach base case [OK]
Common Mistakes:
  • Increasing input causing infinite recursion
  • Missing or wrong base case
  • Changing print statement instead of recursion
5. You want to write a recursive function flatten that takes a nested array of numbers (e.g., [1, [2, [3]], 4]) and returns a flat array [1, 2, 3, 4]. Which approach correctly uses base and recursive cases?
hard
A. Base case: always return input; Recursive case: never call flatten recursively
B. Base case: if input is number, return number; Recursive case: call flatten on input
C. Base case: if input is empty array, return empty array; Recursive case: for each element, if array, call flatten, else add element
D. Base case: if input is array, return input; Recursive case: if input is number, call flatten

Solution

  1. Step 1: Define base case for empty array

    If the input array is empty, return an empty array to stop recursion.
  2. Step 2: Define recursive case for nested arrays

    For each element, if it is an array, recursively call flatten; if not, add element to result.
  3. Final Answer:

    Base case: if input is empty array, return empty array; Recursive case: for each element, if array, call flatten, else add element -> Option C
  4. Quick Check:

    Empty array stops recursion, nested arrays flatten recursively [OK]
Hint: Empty array stops recursion; recurse on nested arrays [OK]
Common Mistakes:
  • Wrong base case returning number instead of array
  • Not checking element type before recursion
  • Infinite recursion by missing base case
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard