Recursion Base Case and Recursive Case in DSA C - Time & Space Complexity
When using recursion, it is important to understand how many times the function calls itself.
We want to know how the number of calls grows as the input size changes.
Analyze the time complexity of the following code snippet.
int factorial(int n) {
if (n == 0) {
return 1; // base case
} else {
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 per call, reducing n by 1 each time.
- How many times: It repeats until n reaches 0, so approximately n times.
Each call does a small amount of work and then calls itself 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 calls.
Time Complexity: O(n)
This means the time to finish 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 call, 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 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 no further calls happen.Final Answer:
To stop the recursion and prevent infinite calls -> Option BQuick Check:
Base case = stop recursion [OK]
- Confusing base case with recursive case
- Thinking base case calls function again
- Ignoring the need for a stopping condition
Solution
Step 1: Identify base case syntax
The base case uses an if condition to check when to stop recursion.Step 2: Check each option
if (n == 0) return 1; uses if (n == 0) return 1; which correctly stops recursion. Others use loops or increase n, which are incorrect.Final Answer:
if (n == 0) return 1; -> Option AQuick Check:
Base case uses if condition [OK]
- Using loops instead of base case
- Increasing input instead of decreasing
- Missing return statement in base case
int factorial(int n) {
if (n == 0) return 1;
else return n * factorial(n - 1);
}
int main() {
printf("%d", factorial(3));
return 0;
}Solution
Step 1: Trace factorial(3) calls
factorial(3) calls factorial(2), factorial(2) calls factorial(1), factorial(1) calls factorial(0).Step 2: Calculate return values
factorial(0) = 1; factorial(1) = 1*1=1; factorial(2) = 2*1=2; factorial(3) = 3*2=6.Final Answer:
6 -> Option AQuick Check:
3! = 6 [OK]
- Not returning 1 for factorial(0)
- Multiplying incorrectly
- Confusing output with input
int sum(int n) {
if (n == 1) return 1;
else return n + sum(n);
}Solution
Step 1: Check base case
Base case is if (n == 1) return 1; which is correct.Step 2: Analyze recursive call
Recursive call is sum(n) which does not reduce n, causing infinite recursion.Final Answer:
Recursive call does not reduce input -> Option DQuick Check:
Recursive input must decrease [OK]
- Calling sum(n) instead of sum(n-1)
- Wrong base case value
- Forgetting to return recursive call
Solution
Step 1: Understand base case for string reversal
Base case stops recursion when string is empty or length 1, returning string as is.Step 2: Understand recursive case
Recursive case returns last character plus reversed substring excluding last character.Final Answer:
Base case: if string is empty or length 1, return string; Recursive case: return last char + reverse(rest of string) -> Option CQuick Check:
Base case stops; recursive case reduces string [OK]
- Wrong base case condition
- Recursive case not reducing string length
- Confusing base and recursive cases
Ready to prove it?
Pass the test and this pattern moves unknown
