Recursion Base Case and Recursive Case in DSA C++ - Time & Space Complexity
We want to understand how the time taken by a recursive function grows as the input size increases.
Specifically, how the base case and recursive calls affect the total work done.
Analyze the time complexity of the following recursive function.
int factorial(int n) {
if (n == 0) return 1; // base case
return n * factorial(n - 1); // recursive case
}
This function calculates the factorial of n by calling itself with smaller values until it reaches zero.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: One recursive call per function call.
- How many times: The function calls itself n times until it reaches the base case.
Each call does a small amount of work and then calls itself with n-1.
| 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 the work grows linearly.
Time Complexity: O(n)
This means the time taken grows in a straight line as the input number increases.
[X] Wrong: "Recursion always takes exponential time because it calls itself multiple times."
[OK] Correct: This function calls itself only once per call, so the time grows linearly, not exponentially.
Understanding how base and recursive cases affect time helps you explain and optimize recursive solutions clearly.
"What if the function made two recursive calls instead of one? How would the time complexity change?"
Practice
base case in a recursive function?Solution
Step 1: Understand recursion basics
Recursion works by calling itself repeatedly until a stopping point is reached.Step 2: Identify the role of base case
The base case stops further recursive calls to avoid infinite loops.Final Answer:
To stop the recursion and prevent infinite calls -> Option DQuick Check:
Base case = stop recursion [OK]
- Confusing base case with recursive case
- Thinking base case calls recursion again
- Ignoring the need for a stopping condition
int factorial(int n) {
if (___) return 1;
return n * factorial(n - 1);
}Solution
Step 1: Identify factorial base case
Factorial of 0 is defined as 1, so base case is when n == 0.Step 2: Check options for correct base case
n == 0 matches n == 0, which returns 1 stopping recursion correctly.Final Answer:
n == 0 -> Option BQuick Check:
Factorial base case = n == 0 [OK]
- Using n > 0 as base case causes infinite recursion
- Using n < 0 which is invalid for factorial
- Confusing base case with recursive case
int sum(int n) {
if (n == 0) return 0;
return n + sum(n - 1);
}
int main() {
std::cout << sum(3);
return 0;
}Solution
Step 1: Trace recursive calls for sum(3)
sum(3) = 3 + sum(2), sum(2) = 2 + sum(1), sum(1) = 1 + sum(0), sum(0) = 0.Step 2: Calculate total sum
sum(3) = 3 + 2 + 1 + 0 = 6.Final Answer:
6 -> Option CQuick Check:
Sum 1 to 3 = 6 [OK]
- Returning n instead of n + sum(n-1)
- Missing base case causing infinite recursion
- Confusing output with just n
int factorial(int n) {
if (n == 1) return 1;
else return n * factorial(n + 1);
}Solution
Step 1: Analyze recursive call
The function calls factorial(n + 1), which increases n each time, never reaching base case.Step 2: Identify infinite recursion cause
Because n grows, base case n == 1 is never met, causing infinite recursion.Final Answer:
Recursive call increases n, causing infinite recursion -> Option AQuick Check:
Recursive call must reduce n [OK]
- Increasing n instead of decreasing in recursion
- Wrong base case value
- Ignoring infinite recursion risk
fib(0) = 0 and fib(1) = 1. Which of the following correctly defines the base cases and recursive case?Solution
Step 1: Identify correct base cases for Fibonacci
By definition, fib(0) = 0 and fib(1) = 1 are base cases.Step 2: Check recursive case correctness
Recursive case sums fib(n-1) and fib(n-2), matching Fibonacci definition.Final Answer:
if (n == 0) return 0; if (n == 1) return 1; else return fib(n - 1) + fib(n - 2); -> Option AQuick Check:
Fibonacci base and recursive cases match definition [OK]
- Wrong base case values
- Increasing n in recursive calls
- Returning wrong values for base cases
Ready to prove it?
Pass the test and this pattern moves unknown
