Recursion Base Case and Recursive Case in DSA Javascript - Time & Space Complexity
When using recursion, it is important to understand how the time taken grows as the input changes.
We want to know how many times the function calls itself before stopping.
Analyze the time complexity of the following recursive function.
function factorial(n) {
if (n === 0) {
return 1; // base case
}
return n * factorial(n - 1); // recursive case
}
This function calculates the factorial of a number by calling itself with smaller values until it reaches zero.
Look for repeated actions in the code.
- Primary operation: The function calls itself once each time with n reduced by 1.
- How many times: It repeats this call until n reaches 0, so about n times.
Each time the function calls itself, it does a small amount of work and then calls again with a smaller number.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 calls |
| 100 | About 100 calls |
| 1000 | About 1000 calls |
Pattern observation: The number of calls grows directly with n, so doubling n doubles the work.
Time Complexity: O(n)
This means the time taken grows in a straight line with the input size.
[X] Wrong: "Recursion always means exponential time because it calls itself multiple times."
[OK] Correct: This example calls itself only once per step, so it grows linearly, not exponentially.
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 function called itself twice each time 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 no further calls happen.Final Answer:
To stop the recursion and prevent infinite calls -> Option CQuick Check:
Base case = stop recursion [OK]
- Confusing base case with recursive case
- Thinking base case calls recursion again
- Ignoring base case leads to infinite recursion
function factorial(n) {
if (___) {
return 1;
}
return n * factorial(n - 1);
}Solution
Step 1: Recall factorial base case
Factorial of 0 is 1, so base case is when n equals 0.Step 2: Match condition to code
The conditionn === 0correctly stops recursion and returns 1.Final Answer:
n === 0 -> Option BQuick Check:
Base case for factorial = n === 0 [OK]
- Using n > 0 as base case causes infinite recursion
- Using n < 0 which is invalid for factorial
- Using n === 1 misses factorial(0) base case
function sumTo(n) {
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 total sum
sumTo(3) = 3 + 2 + 1 + 0 = 6.Final Answer:
6 -> Option AQuick Check:
3+2+1+0 = 6 [OK]
- Forgetting to add current n before recursive call
- Confusing base case return value
- Assuming output is just n instead of sum
function fib(n) {
if (n === 0) return 0;
if (n === 1) return 1;
return fib(n - 1) + fib(n - 2);
}
console.log(fib(-1));Solution
Step 1: Analyze base cases and input
Base cases cover n=0 and n=1, but input is n=-1 which is not handled.Step 2: Understand recursion with negative input
Calling fib(-1) leads to fib(-2) and fib(-3) calls infinitely, no stop condition.Final Answer:
Missing base case for negative n causing infinite recursion -> Option AQuick Check:
Negative input needs base case [OK]
- Ignoring negative inputs in recursion
- Assuming fib(-1) is valid without base case
- Changing recursive calls incorrectly
function reverse(str) {
if (___) {
return ___;
}
return reverse(___) + str[0];
}Solution
Step 1: Identify base case for empty string
When string is empty (''), return the empty string to stop recursion.Step 2: Define recursive call correctly
Call reverse on substring from index 1 to end, then add first character at the end.Final Answer:
str === '' / str / str.slice(1) -> Option DQuick Check:
Empty string base case and slice(1) recursive call [OK]
- Using null instead of empty string as base case
- Returning wrong substring in recursive call
- Incorrect base case condition
Ready to prove it?
Pass the test and this pattern moves unknown
