Recursion Basics Java | Fresher Interview
Fast recall
Quick revision
Core answer
StackOverflowError.Whether you think about the base case first — a sign of disciplined recursive thinking
Writing the recursive case first and forgetting the base case, causing a StackOverflowError
Use factorial as an example, then explain the call stack to show you understand the execution model
Interview-ready guide
Interview answer
Recursion is when a method calls itself to solve a problem by breaking it into smaller identical pieces.
There are two essential parts you can't skip. First is the base case — that's your stopping condition. Without it, the function recurses forever and you get a StackOverflowError.
Second is the recursive case, which is where you call the function again but with a smaller input that moves you closer to the base case. Let me give you factorial as an example. The base case is when n equals one or zero, you just return one.
The recursive case is returning n times factorial of n minus one. So if you call factorial of five, it computes five times factorial of four, which computes four times factorial of three, and so on down to factorial of one. That's five stack frames deep.
Once it hits the base case, the stack unwinds and each call returns its result back up. The key thing to understand is the call stack. Every recursive call creates a new stack frame with its own copy of the variables.
Those frames pile up until the base case, then pop off as results return. That's why deep recursion can cause stack overflow in Java — the JVM has a limited stack size. The base case and the smaller recursive input should be checked together.
For factorial(3), the calls must visibly progress through 3, 2, 1 and then stop before the stack unwinds.
Recursion Basics Java | Fresher Interview
Direct Answer
Recursion is when a method calls itself to solve a smaller version of the same problem. Every recursive solution needs two parts: a base case (the stopping condition) and a recursive case (which shrinks the problem toward the base case). Missing either causes infinite recursion and a StackOverflowError.
Code Example
public class RecursionDemo {
public static int factorial(int n) {
if (n <= 1) { // base case: factorial of 0 or 1 is 1
return 1;
}
return n * factorial(n - 1); // recursive case: n! = n * (n-1)!
}
public static void main(String[] args) {
System.out.println("5! = " + factorial(5)); // 120
}
}
// Dry run factorial(3):
// factorial(3) = 3 * factorial(2)
// factorial(2) = 2 * factorial(1)
// factorial(1) = 1 <- base case, stack starts unwinding
// => 2 * 1 = 2 => 3 * 2 = 6
Common Mistakes & Pitfalls
- Forgetting the base case: without a stopping condition, calls continue until
StackOverflowError. - Wrong base case logic: checking
n == 0when you meantn <= 0causes one extra incorrect call. - Not reducing toward the base case: if the recursive call doesn't make progress (like
factorial(n)instead offactorial(n-1)), you get infinite recursion. Always trace a small input like n=3 on paper to verify the base case is reachable.
Check your understanding
Answer these without looking back at the article.
- What is the difference between recursion and iteration?
- How do you convert a recursive solution to iterative?
- What causes a StackOverflowError?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.