Recursion Base Case and Recursive Case in DSA Java - 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 changes.
Analyze the time complexity of the following code snippet.
public 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 waits for the next call to finish, so the total calls grow as n grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 calls |
| 100 | About 100 calls |
| 1000 | About 1000 calls |
Pattern observation: The number of calls grows linearly with n.
Time Complexity: O(n)
This means the time to complete grows directly in proportion to the input size.
[X] Wrong: "Recursion always means exponential time because it calls itself multiple times."
[OK] Correct: In this case, the function calls itself only once per call, so the growth is linear, not exponential.
Understanding how base and recursive cases affect time helps you explain your code clearly and shows you know how recursion scales.
"What if the recursive case called the function twice instead of once? How would the time complexity change?"
Practice
base case?Solution
Step 1: Understand the base case role
The base case is the condition where recursion stops to avoid infinite calls.Step 2: Differentiate base case from recursive case
The recursive case calls the function again, but the base case ends recursion.Final Answer:
To stop the recursion and prevent infinite calls -> Option DQuick Check:
Base case stops recursion [OK]
- Confusing base case with recursive case
- Thinking base case calls function again
- Ignoring base case leads to infinite recursion
Solution
Step 1: Identify base case condition for factorial
Factorial base case is when n equals 0, return 1.Step 2: Check syntax correctness
if (n == 0) return 1; correctly returns 1 when n == 0, stopping recursion.Final Answer:
if (n == 0) return 1; -> Option CQuick Check:
Base case for factorial is n == 0 [OK]
- Using wrong condition for base case
- Calling factorial with n + 1 causing infinite recursion
- Not returning a value in base case
public class Test {
public static int sum(int n) {
if (n == 0) return 0;
return n + sum(n - 1);
}
public static void main(String[] args) {
System.out.println(sum(3));
}
}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 BQuick Check:
Sum 1+2+3 = 6 [OK]
- Confusing base case return value
- Adding n - 1 instead of n
- Expecting output 3 instead of sum
public static int factorial(int n) {
if (n == 0) return 0;
return n * factorial(n - 1);
}Solution
Step 1: Check base case return value
Factorial base case should return 1 for n == 0, but code returns 0.Step 2: Understand impact of wrong base case
Returning 0 causes factorial to always be 0 after recursion.Final Answer:
Base case returns 0 instead of 1 -> Option AQuick Check:
Factorial base case must return 1 [OK]
- Returning 0 in base case
- Using wrong recursive call direction
- Forgetting return keyword
public static int fib(int n) {
if (n <= 1) return n;
return fib(n - 1) + fib(n - 2);
}What is the output of
fib(4) and explain why the base case is important here?Solution
Step 1: Calculate fib(4) using recursion
fib(4) = fib(3) + fib(2); fib(3) = fib(2) + fib(1); fib(2) = fib(1) + fib(0); fib(1)=1, fib(0)=0.Step 2: Compute values step-by-step
fib(2)=1+0=1; fib(3)=1+1=2; fib(4)=2+1=3. Base case returns n when n ≤ 1, stopping recursion.Final Answer:
3; base case stops infinite recursion by returning n when n ≤ 1 -> Option AQuick Check:
fib(4) = 3 with base case stopping recursion [OK]
- Miscomputing fib(4) as 5 or 7
- Ignoring base case importance
- Assuming base case returns 0 always
Ready to prove it?
Pass the test and this pattern moves unknown
