Recursion Base Case and Recursive Case in DSA Typescript - Time & Space Complexity
When using recursion, it is important to know how many times the function calls itself before stopping.
We want to find out how the number of calls grows as the input gets bigger.
Analyze the time complexity of the following code snippet.
function factorial(n: number): number {
if (n === 0) {
return 1; // base case
}
return n * factorial(n - 1); // recursive case
}
This code calculates the factorial of a number using recursion with a base case and a recursive case.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The function calls itself once each time with a smaller number.
- How many times: It repeats until the input reaches zero, so about n times.
Each time the function calls itself, it does one multiplication and one smaller call.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 calls and 10 multiplications |
| 100 | About 100 calls and 100 multiplications |
| 1000 | About 1000 calls and 1000 multiplications |
Pattern observation: The number of operations grows directly with the input size.
Time Complexity: O(n)
This means the time to finish grows in a straight line as the input number gets bigger.
[X] Wrong: "Recursion always makes the program slow and complex."
[OK] Correct: Simple recursion like this runs in linear time and is easy to understand when base cases stop the calls.
Understanding how base and recursive cases affect time helps you explain your code clearly and shows you know how recursion works efficiently.
"What if the recursive case called the function twice instead of once? How would the time complexity change?"
Practice
base case in a recursive function?Solution
Step 1: Understand what recursion does
Recursion calls the same function repeatedly with smaller inputs.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.Final Answer:
To stop the recursion and prevent infinite calls -> Option BQuick Check:
Base case = stop recursion [OK]
- Thinking base case calls recursion again
- Confusing base case with recursive case
- Ignoring base case leads to infinite recursion
Solution
Step 1: Check base case correctness
function factorial(n: number): number { if (n === 0) return 1; else return n * factorial(n - 1); } usesif (n === 0) return 1;which is correct for factorial base case.Step 2: Check recursive call correctness
function factorial(n: number): number { if (n === 0) return 1; else return n * factorial(n - 1); } callsfactorial(n - 1)and multiplies byn, which matches factorial definition.Final Answer:
function factorial(n: number): number { if (n === 0) return 1; else return n * factorial(n - 1); } -> Option AQuick Check:
Correct base and recursive case syntax = function factorial(n: number): number { if (n === 0) return 1; else return n * factorial(n - 1); } [OK]
- Using while loop instead of recursion
- Wrong base case value
- Recursive call increasing n causing infinite recursion
function sumTo(n: number): number {
if (n === 0) return 0;
return n + sumTo(n - 1);
}
console.log(sumTo(3));Solution
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.Step 2: Calculate return values
sumTo(1) = 1 + 0 = 1, sumTo(2) = 2 + 1 = 3, sumTo(3) = 3 + 3 = 6.Final Answer:
6 -> Option AQuick Check:
Sum 1+2+3 = 6 [OK]
- Confusing base case return value
- Adding instead of multiplying
- Missing base case causing infinite recursion
function countdown(n: number): void {
if (n === 0) return;
console.log(n);
countdown(n + 1);
}
countdown(3);Solution
Step 1: Analyze recursive call direction
The function callscountdown(n + 1), which increases n, causing infinite recursion.Step 2: Fix recursive call to decrease n
Changing tocountdown(n - 1)moves towards base casen === 0, stopping recursion.Final Answer:
Changecountdown(n + 1)tocountdown(n - 1)-> Option DQuick Check:
Recursive call must approach base case [OK]
- Increasing input causing infinite recursion
- Missing or wrong base case
- Changing print statement instead of recursion
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?Solution
Step 1: Define base case for empty array
If the input array is empty, return an empty array to stop recursion.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.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 CQuick Check:
Empty array stops recursion, nested arrays flatten recursively [OK]
- Wrong base case returning number instead of array
- Not checking element type before recursion
- Infinite recursion by missing base case
Ready to prove it?
Pass the test and this pattern moves unknown
