Recursive Methods
A recursive method is a method that calls itself to solve a problem by breaking it into smaller subproblems. Recursion is powerful for problems that have a natural repetitive structure. This is a high-value interview topic and essential for understanding algorithms, stack behavior, and problem decomposition.
What Is a Recursive Method?
- A method that invokes itself
- Solves a problem using smaller instances of the same problem
- Must have a base condition to stop execution
void recursiveMethod() {
recursiveMethod();
}
⚠️ This example causes infinite recursion (no base case).
Two Mandatory Components of Recursion
1. Base Case (Termination Condition)
- Stops recursive calls
- Prevents infinite recursion
if (n == 0) {
return;
}
2. Recursive Case
- Method calls itself with modified arguments
print(n - 1);
Simple Example: Print Numbers (1 to N)
void print(int n) {
if (n == 0) {
return;
}
print(n - 1);
System.out.println(n);
}
Call: print(3)
Output:
1
2
3
Example: Factorial Using Recursion
Mathematical Definition
n! = n × (n-1)!
0! = 1
Java Code
int factorial(int n) {
if (n == 0) {
return 1;
}
return n * factorial(n - 1);
}
Example: Fibonacci Series (Classic Interview Question)
int fib(int n) {
if (n == 0 || n == 1) {
return n;
}
return fib(n - 1) + fib(n - 2);
}
⚠️ Not efficient for large n due to repeated calls.
Recursive Call Execution (Call Stack Concept)
For factorial(3):
factorial(3)
└─ factorial(2)
└─ factorial(1)
└─ factorial(0)
Then results return backward.
Recursion vs Iteration
| Feature | Recursion | Iteration |
|---|---|---|
| Code readability | High (for some problems) | High |
| Memory usage | Higher (stack) | Lower |
| Performance | Slower | Faster |
| Complexity | Elegant logic | More control |
| Risk | StackOverflowError | Infinite loop |
StackOverflowError (Important)
Occurs when:
- Base case is missing
- Recursive depth is too large
Exception in thread "main" java.lang.StackOverflowError
Tail Recursion (Conceptual)
A recursion where the recursive call is the last statement.
void print(int n) {
if (n == 0) return;
System.out.println(n);
print(n - 1);
}
⚠️ Java does not optimize tail recursion automatically.
When to Use Recursion
- Tree and graph traversal
- Divide-and-conquer algorithms
- Mathematical problems
- Backtracking problems
When NOT to Use Recursion
- Very large input size
- Performance-critical loops
- Simple repetitive logic
- Risk of deep recursion
Common Beginner Mistakes
- Missing base case
- Wrong base condition
- Infinite recursion
- Expecting recursion to be faster
- Ignoring stack memory usage
Interview-Ready Answers
Short Answer
A recursive method is a method that calls itself to solve a problem in smaller steps.
Detailed Answer
In Java, recursion involves a method invoking itself with a base condition to terminate execution. Each recursive call is stored in the call stack, and results return in reverse order. Proper base conditions are essential to avoid StackOverflowError.
Key Takeaway
Recursive methods provide elegant solutions for naturally recursive problems, but they must be used carefully due to memory and performance considerations.