Java Notes
Master recursion — base cases, recursive thinking, call stack visualization, tail recursion, memoization, backtracking, and converting recursion to iteration.
What is Recursion?
A method that calls itself to solve a problem by breaking it into smaller subproblems of the same type. Every recursive solution has:
- Base case — stops the recursion (prevents infinite loop)
- Recursive case — reduces the problem and calls itself
// Structure of every recursive function
returnType solve(problem) {
if (baseCase) {
return simpleSolution; // Stop here
}
// Break into smaller problem
return combine(solve(smallerProblem)); // Self-call
}Classic Recursive Problems
Fibonacci
Power Function
// Naive: O(n)
double power(double base, int exp) {
if (exp == 0) return 1;
if (exp < 0) return 1.0 / power(base, -exp);
return base * power(base, exp - 1);
}
// Fast power: O(log n) — divide and conquer
double fastPower(double base, int exp) {
if (exp == 0) return 1;
if (exp < 0) return 1.0 / fastPower(base, -exp);
double half = fastPower(base, exp / 2);
if (exp % 2 == 0) {
return half * half; // Even: x^n = (x^(n/2))²
} else {
return half * half * base; // Odd: x^n = (x^(n/2))² × x
}
}Tower of Hanoi
void towerOfHanoi(int n, char from, char to, char aux) {
if (n == 1) {
System.out.println("Move disk 1 from " + from + " to " + to);
return;
}
towerOfHanoi(n - 1, from, aux, to); // Move n-1 disks to auxiliary
System.out.println("Move disk " + n + " from " + from + " to " + to);
towerOfHanoi(n - 1, aux, to, from); // Move n-1 disks from auxiliary to target
}
// Time: O(2^n), Space: O(n) stackBacktracking
Generating All Subsets
Recursion vs Iteration
Interview Questions
Q1: What are the two essential parts of a recursive function?
Answer: Base case (termination condition that returns without recursing) and recursive case (breaks problem into smaller subproblem and calls itself). Missing a base case causes StackOverflowError.
Q2: What is the difference between head and tail recursion?
Answer: Head recursion: recursive call happens before processing (e.g., solve(n-1); process(n);). Tail recursion: recursive call is the LAST operation (e.g., return solve(n-1, accumulator);). Tail recursion can be optimized to iteration by compilers (not JVM though).
Q3: How does memoization improve recursive solutions?
Answer: It caches previously computed results. Without memo, fibonacci(50) makes ~2^50 calls (duplicate subproblems). With memo, each subproblem is solved once and stored — reducing to O(n) calls.
Q4: What is the maximum recursion depth in Java?
Answer: Depends on stack size (default ~512KB-1MB, roughly 5000-10000 frames). Can be increased with -Xss JVM flag. For deeper recursion, convert to iterative with explicit stack.
Q5: Explain backtracking with an example.
Answer: Backtracking explores solutions incrementally, abandoning paths that violate constraints. Pattern: Choose → Explore (recurse) → Un-choose (backtrack). Example: N-Queens places queens row by row, backs up when no safe column exists.
Q6: When should you use recursion vs iteration?
Answer: Recursion: tree/graph traversal, divide-and-conquer, backtracking, problems with recursive structure (Towers of Hanoi). Iteration: simple loops, performance-critical code, deep recursion risks. Many interviewers expect recursive solutions.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Recursion in Java.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Java Master Course topic.
Search Terms
java-master-course, java master course, java, master, course, dsa, recursion, recursion in java
Related Java Master Course Topics