Mid-level 4 min · March 06, 2026

Recursion Interview Problems - Symlink Cycle StackOverflow

A self-referencing symlink caused StackOverflowError after 5000 recursive calls in a backup agent.

N
Naren · Founder
Plain-English first. Then code. Then the interview question.
About
 ● Production Incident 🔎 Debug Guide
Quick Answer
  • Recursion solves problems by breaking them into smaller self-similar subproblems
  • Base case is the termination condition — missing it causes infinite recursion and StackOverflowError
  • Four core patterns: Linear, Binary, Tail, Divide-and-Conquer
  • Memoisation turns exponential binary recursion into linear time
  • Backtracking follows make choice → recurse → undo choice; skipping undo corrupts branches
  • Production risk: deep recursion (depth > 10,000) crashes JVM; prefer iteration or Trampoline for large inputs
Plain-English First

Imagine you're looking for your lost keys in a house with many rooms. You open room 1, and instead of searching it yourself, you hire a clone of yourself to search it while you move on — but that clone does the same thing, hiring another clone for the next room. Each clone only needs to know one rule: 'search this room, then report back.' That's recursion. A function solves a tiny piece of a problem, then calls a copy of itself to handle the rest — until there's nothing left to solve.

Recursion shows up in almost every senior engineering interview. It's not because interviewers love brain teasers — it's because recursion is the natural language of problems that have self-similar structure: file systems, tree traversals, divide-and-conquer algorithms, and dynamic programming all lean on it. Companies like Google, Meta, and Amazon use recursion problems specifically because they reveal whether a candidate can break a complex problem into a repeatable, trustworthy unit of work.

The real problem most developers have with recursion isn't the concept — it's pattern blindness. They see a new problem and freeze because it looks unique, when in reality it fits one of four classic shapes. Once you can spot the shape, the solution almost writes itself. The mental shift is from 'how do I solve this whole thing?' to 'what's the one small thing I need to do right now, and can I trust the rest to a recursive call?'

By the end of this article you'll be able to identify which recursion pattern a problem belongs to, write clean base cases that don't blow the stack, trace recursive calls mentally without getting lost, and confidently explain your reasoning to an interviewer — which is half the battle.

The Four Recursion Patterns Every Interviewer Tests

Every recursion problem in an interview maps to one of four patterns. Learning to spot the pattern is more valuable than memorising solutions.

Pattern 1 — Linear Recursion: One recursive call per invocation. Classic examples: factorial, sum of a list, reversing a string. The call stack grows linearly with input size.

Pattern 2 — Binary/Branching Recursion: Two recursive calls per invocation. Fibonacci, binary tree traversal, merge sort. The call tree fans out and this is where O(2^n) danger lives if you're not careful.

Pattern 3 — Tail Recursion: The recursive call is the very last operation. Many languages and compilers can optimise this into a loop, killing stack overhead. Java doesn't do this automatically — but interviewers love asking about it.

Pattern 4 — Divide and Conquer: Split the problem into independent halves, recurse on each, then combine results. Merge sort and quicksort live here.

Knowing which pattern you're in tells you immediately what your time complexity will be and whether you need memoisation. A binary recursion with overlapping subproblems screams 'add a cache'. A linear recursion on a million-element list screams 'convert to iteration'.

io/thecodeforge/recursion/RecursionPatterns.javaJAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package io.thecodeforge.recursion;

/**
 * Production-grade examples of the 4 recursion patterns.
 */
public class RecursionPatterns {

    // PATTERN 1: Linear Recursion (Summing elements)
    public static int sumOfList(int[] numbers, int currentIndex) {
        if (currentIndex == numbers.length) return 0;
        return numbers[currentIndex] + sumOfList(numbers, currentIndex + 1);
    }

    // PATTERN 2: Binary Recursion (Naive Fibonacci - O(2^n))
    public static int fibonacciNaive(int position) {
        if (position <= 1) return position;
        return fibonacciNaive(position - 1) + fibonacciNaive(position - 2);
    }

    // PATTERN 3: Tail Recursion (Factorial with Accumulator)
    public static int factorialTailRecursive(int number, int accumulator) {
        if (number == 0) return accumulator;
        return factorialTailRecursive(number - 1, accumulator * number);
    }

    // PATTERN 4: Divide and Conquer (Recursive Binary Search)
    public static int binarySearch(int[] sortedNumbers, int target, int low, int high) {
        if (low > high) return -1;
        int mid = low + (high - low) / 2;
        if (sortedNumbers[mid] == target) return mid;
        return (sortedNumbers[mid] < target) 
            ? binarySearch(sortedNumbers, target, mid + 1, high) 
            : binarySearch(sortedNumbers, target, low, mid - 1);
    }
}
Output
Standard recursion patterns demonstrated with base and recursive cases.
Pattern Recognition Shortcut:
Before writing a single line of code in an interview, say out loud: 'This looks like [pattern] because [reason].' Naming the pattern signals senior-level thinking and gives you a structural blueprint before you start coding.
Production Insight
Binary recursion without memoisation explodes call count. Fibonacci naive calls itself O(2^n) times.
At n=40 that's over 1 billion calls — your app dies or the interview ends.
Rule: spot overlapping subproblems early and add a cache before you code the recursion.
Key Takeaway
Pattern recognition is faster than solution memorisation.
Name the pattern first, then write the code.
If calls branch, think memoisation.
Which Pattern to Use?
IfOne recursive call, no overlapping subproblems
UseLinear recursion — O(n) space, no memoisation needed
IfTwo or more recursive calls, overlapping subproblems
UseBinary recursion with memoisation — turns O(2^n) into O(n)
IfRecursive call is last operation, no post-processing
UseTail recursion — Java doesn't optimise, but interviewers want to see you know the concept
IfProblem requires dividing input into independent halves
UseDivide-and-conquer — O(n log n) typical, no overlap, combine step is key

Solving the Classic Tree Problems Interviewers Love

Binary tree problems are the most common recursion questions in FAANG-style interviews. They feel hard until you realise every single one follows the same three-line mental model: handle the null base case, do something at this node, recurse left and right.

The secret is trusting your recursive calls. The biggest mistake candidates make is trying to mentally trace the entire recursion in their head. You don't need to. You only need to answer: 'If my recursive call correctly returns the answer for a subtree, what do I do with it at this node?'

Take finding the maximum depth of a binary tree. At each node, you trust that maxDepth(node.left) gives you the correct depth of the left subtree and maxDepth(node.right) gives you the right. Your only job at this node is: 1 + max(leftDepth, rightDepth). That's it. The whole algorithm is that one insight.

io/thecodeforge/recursion/BinaryTreeRecursion.javaJAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package io.thecodeforge.recursion;

public class BinaryTreeRecursion {
    static class TreeNode {
        int val; TreeNode left, right;
        TreeNode(int x) { val = x; }
    }

    // LeetCode #104: Maximum Depth of Binary Tree
    public int maxDepth(TreeNode root) {
        if (root == null) return 0;
        return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
    }

    // LeetCode #100: Same Tree
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if (p == null && q == null) return true;
        if (p == null || q == null || p.val != q.val) return false;
        return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
    }
}
Output
Standard Tree recursion solutions passing LeetCode test cases.
Interview Gold:
Interviewers watch HOW you handle null. Robust base cases for null nodes are what separate 'can code' from 'thinks in edge cases'. Always ask yourself: what happens if the root itself is null?
Production Insight
Tree recursion in production rarely uses recursion — trees can be 100k+ nodes deep, causing stack overflow.
Real systems use iterative DFS with explicit stack or BFS.
Rule: recursion works for balanced trees; worst-case (linked list) kills it.
Key Takeaway
Trust the recursion: subtree answers are correct by definition.
Your job is only to combine them at this node.
If tree depth is unbounded, use iteration.

Memoisation — Turning Exponential Recursion into Linear

Naive binary recursion on problems with overlapping subproblems is a performance disaster. fibonacciNaive(40) makes over a billion function calls. The fix — memoisation — is one of the highest-value techniques you can demonstrate in an interview because it shows you understand both recursion and dynamic programming.

The insight is simple: if you've already computed the answer for a subproblem, cache it. On the next call with the same input, return the cached answer instantly instead of recomputing from scratch.

io/thecodeforge/recursion/MemoizedRecursion.javaJAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package io.thecodeforge.recursion;
import java.util.HashMap;
import java.util.Map;

public class MemoizedRecursion {
    private Map<Integer, Integer> memo = new HashMap<>();

    // Climbing Stairs (LeetCode #70) - O(n) Time, O(n) Space
    public int climbStairs(int n) {
        if (n <= 2) return n;
        if (memo.containsKey(n)) return memo.get(n);
        
        int result = climbStairs(n - 1) + climbStairs(n - 2);
        memo.put(n, result);
        return result;
    }
}
Output
Memoized approach reducing complexity from exponential to linear.
Watch Out:
Sharing a memoisation cache across test runs causes subtle bugs — if you call fibonacciMemoized in multiple tests, the cache from run 1 persists into run 2. In production code or unit tests, pass the cache as a parameter or clear it between calls.
Production Insight
Memoisation works brilliantly for deterministic functions with integer inputs.
But caching on large input spaces (e.g., matrix coordinates) blows memory.
Rule: know when to switch to tabulation — bottom-up DP uses less space.
Key Takeaway
Cache recursive results to cut exponential branches.
Cache key = function arguments that change.
If cache size grows unbounded, switch to tabulation.

Backtracking — Recursion That Knows How to Undo Its Mistakes

Backtracking is recursion with a safety net. It explores a path, and if that path leads nowhere, it undoes the last step and tries another direction. Think of it like solving a maze by always drawing on the walls with chalk — if you hit a dead end, you erase back to the last junction and try a different turn.

Backtracking shows up in: generating all permutations, solving Sudoku, the N-Queens problem, and finding all subsets of a set. These are problems where you're building a solution piece-by-piece and some partial solutions turn out to be invalid.

io/thecodeforge/recursion/BacktrackingProblems.javaJAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package io.thecodeforge.recursion;
import java.util.*;

public class BacktrackingProblems {
    // Subsets (LeetCode #78)
    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> list = new ArrayList<>();
        backtrack(list, new ArrayList<>(), nums, 0);
        return list;
    }

    private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums, int start){
        list.add(new ArrayList<>(tempList)); // Capture snapshot
        for(int i = start; i < nums.length; i++){
            tempList.add(nums[i]);
            backtrack(list, tempList, nums, i + 1);
            tempList.remove(tempList.size() - 1); // The Backtrack Step
        }
    }
}
Output
Generates all unique subsets of a set using the standard backtracking template.
Pro Tip:
When adding a collection to your results list in backtracking, always pass new ArrayList<>(currentSubset) — not currentSubset itself. Passing the reference means every result in your list will point to the same object and mutate as you backtrack. This is the #1 silent bug in backtracking code.
Production Insight
Backtracking scales poorly — N-Queens for N=25 still runs for hours without pruning.
Production solutions use constraint propagation (e.g., forward-checking) or SAT solvers.
Rule: backtracking is for small search spaces; for larger ones, add pruning early.
Key Takeaway
Make a choice, recurse, then undo (explicitly).
Snapshot results, not references.
Prune early or your code won't finish.

Recursion vs Iteration: When to Use Which in Production

Recursion is elegant for problems that mirror the data structure (trees, graphs). But production systems often ban recursion for anything that handles user input because of stack safety. Iteration with an explicit stack (or queue) gives you control over memory and avoids the JVM's fixed stack size.

Key trade-offs
  • Recursion: cleaner code, but O(depth) stack space, risk of overflow for deep inputs.
  • Iteration: more verbose, but O(1) extra space (if no manual stack), no overflow risk.
  • Memoisation works naturally with recursion but can be replicated with tables in iteration (bottom-up DP).
  • Backtracking is nearly always implemented recursively because tracking state with a manual stack is painful.

In interviews, you can start with recursion for clarity, then mention you'd convert to iteration if depth is a concern. That's senior-level thinking.

io/thecodeforge/recursion/RecursionVsIteration.javaJAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package io.thecodeforge.recursion;

import java.util.*;

public class RecursionVsIteration {
    // Recursive DFS on a tree
    public void dfsRecursive(TreeNode node) {
        if (node == null) return;
        System.out.println(node.val);
        dfsRecursive(node.left);
        dfsRecursive(node.right);
    }

    // Iterative DFS (uses explicit stack)
    public void dfsIterative(TreeNode root) {
        if (root == null) return;
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            TreeNode node = stack.pop();
            System.out.println(node.val);
            if (node.right != null) stack.push(node.right);
            if (node.left != null) stack.push(node.left);
        }
    }

    static class TreeNode {
        int val; TreeNode left, right;
        TreeNode(int x) { val = x; }
    }
}
Output
Both produce same traversal order. Iteration uses heap memory (stack) instead of call stack.
Mental Model: Recursion as a Hidden Stack
  • Recursion uses the JVM's call stack — size fixed at startup (default ~1024 KB).
  • Iteration lets you use the heap for the manual stack — heap can grow much larger.
  • Recursion is the 'free' stack; iteration is 'explicit' stack.
  • If depth > 10,000, recursion fails; iteration with heap stack handles millions.
Production Insight
In a real backend, recursive XML/JSON parsing caused nightly OutOfMemoryErrors because deep nesting blew the stack.
We rewrote the parser using a SAX-style iterative approach with an explicit depth limit.
Rule: never use recursion for parsing untrusted nested input — depth is unbounded.
Key Takeaway
Recursion wins for readability on small, balanced structures.
Iteration wins for safety on large or deep inputs.
Mention both in interviews to show you understand the trade-off.
● Production incidentPOST-MORTEMseverity: high

Infinite Recursion via Symlink in File System Scanner

Symptom
Backup agent started throwing java.lang.StackOverflowError on certain directory paths. No files were backed up. Monitoring showed the agent process crashed and restarted repeatedly, burning CPU without progress.
Assumption
The recursion depth would be bounded by the maximum file system depth (typically < 1000). The code had a base case for null directory, but not for detecting already-visited paths.
Root cause
An administrator created a symbolic link from /data/backups/current -> /data/backups/current/../ (self-reference). The recursive traversal visited the same directory infinitely. With ~5000 recursive calls the JVM stack exhausted.
Fix
Added a Set<Path> to track visited directories and skip any directory already visited. Also added a configurable max depth (default 500) to bail out regardless. Deployed as a hotfix within 30 minutes.
Key lesson
  • Never trust that input structure is acyclic — always apply cycle detection in recursion that traverses user-provided data.
  • Add a maximum recursion depth guard even for well-known structures. The JVM stack isn't infinite.
  • Log recursion depth periodically in production loops to detect abnormal growth before it blows the stack.
Production debug guideHow to identify and fix stack overflow, infinite recursion, and performance problems in recursive code.4 entries
Symptom · 01
java.lang.StackOverflowError (or segmentation fault in C/C++)
Fix
Check stack trace for repeating call frames — that indicates the base case is missing or never reached. Add logging at function entry to print depth and arguments. Temporary fix: increase JVM stack size with -Xss4m, but fix the code.
Symptom · 02
Recursive function runs forever (app hangs)
Fix
Check if the recursive call changes the input in a way that eventually hits base case. For example, in a tree traversal, ensure you pass left/right child, not the same node. Add a depth counter and break after 10,000 calls.
Symptom · 03
Exponential slowdown (binary recursion on overlapping subproblems)
Fix
Profile call count using a simple counter. If same arguments are recomputed many times, add memoisation (cache). Example: Fibonacci without cache calls itself over 1 billion times for n=40; with cache, it's O(n).
Symptom · 04
Incorrect results due to shared state mutation
Fix
Look for global or instance variables modified inside recursion. Each recursive call should operate on its own copy or explicitly undo changes (backtracking). Use immutable data structures where possible.
★ Recursion Stack Overflow: Immediate Debug StepsWhen a recursive function crashes with StackOverflowError, run these commands to diagnose and fix.
StackOverflowError on large input
Immediate action
Check if base case is correct. Print the input argument at start of function (System.out.println).
Commands
java -Xss4m -jar app.jar (temporarily increase stack, then fix the bug)
jstack <pid> | grep -A 10 "StackOverflowError" (inspect call stack)
Fix now
Add a depth parameter and throw custom exception if depth > 10000. Or convert to iteration if depth is unbounded.
Infinite recursion but no crash (app hangs)+
Immediate action
Attach debugger or add print statements. Look for arguments that don't approach base case.
Commands
jstack <pid> (see which thread is stuck, examine stack frames)
jcmd <pid> Thread.print (alternative thread dump)
Fix now
Add a max recursion depth guard: if (depth > 10000) throw new RuntimeException("Max depth exceeded");
Recursion vs Iteration in Interviews
AspectRecursionIteration
Readability on tree/graph problemsNatural — mirrors the data structureRequires manual stack management
Space complexityO(n) call stack minimumO(1) if no auxiliary stack needed
Risk of stack overflowYes — deep inputs crash with StackOverflowErrorNo — heap memory is much larger
Performance overheadFunction call overhead per frameNo call overhead — tighter loops
Best used whenProblem has self-similar/recursive structureProblem is sequential or stack depth is a concern
Debugging difficultyHarder — must trace call frames mentallyEasier — state is explicit in variables
Memoisation (DP)Natural fit — cache by function argumentsPossible but requires explicit table setup
Tail-call optimizationJava lacks it; other languages (Scala, Kotlin) may haveNot applicable — loops don't need it

Key takeaways

1
Every recursion problem fits one of four patterns
Linear, Binary, Tail, or Divide-and-Conquer. Naming the pattern before coding is a habit that separates strong candidates from average ones.
2
Base cases are not an afterthought
write them first. A missing base case always causes a StackOverflowError and is the most common recursion bug in interviews.
3
Backtracking follows a strict three-step loop
make a choice, recurse, unmake the choice. Skipping the 'unmake' step silently corrupts all future branches.
4
Memoisation converts overlapping subproblems from exponential to linear time
but always pass a snapshot (new ArrayList), never a reference, when storing intermediate results.
5
Recursion is elegant for interviews; iteration is safer for production. Know the trade-off and explain it.

Common mistakes to avoid

3 patterns
×

Missing or wrong base case

Symptom
Function recurses forever and throws StackOverflowError with no obvious cause. The stack trace shows repeated identical frames.
Fix
Write the base case first. Ask: 'What is the simplest input where I know the answer without recursing?' For a list: empty list. For a tree: null node. For a number: 0 or 1. Then test that case in isolation.
×

Mutating shared state without backtracking

Symptom
Results list contains duplicates, partial results, or all entries look identical. In backtracking, the final list may have multiple copies of the same last state.
Fix
Whenever you modify a shared data structure (list, StringBuilder, array) inside a recursive call, undo that modification immediately after the recursive call returns. Make choice → recurse → unmake choice. Every time.
×

Storing a reference instead of a snapshot in backtracking

Symptom
All entries in your results list end up being identical (usually empty or the last state). The list seems to change retroactively.
Fix
When you do results.add(currentList), you're storing a reference to a list that will keep changing. Always do results.add(new ArrayList<>(currentList)) to capture the state at that moment.
INTERVIEW PREP · PRACTICE MODE

Interview Questions on This Topic

Q01JUNIOR
Explain the concept of 'Stack Overflow' in the context of recursion. How...
Q02SENIOR
Implement the 'Word Search' (LeetCode 79) problem. How would you optimiz...
Q03SENIOR
Compare Top-Down Memoization vs. Bottom-Up Tabulation. Which one would y...
Q04SENIOR
What is Tail Call Optimization (TCO)? Does Java support it, and if not, ...
Q05SENIOR
Solve the 'N-Queens' problem. What is the time complexity of a backtrack...
Q01 of 05JUNIOR

Explain the concept of 'Stack Overflow' in the context of recursion. How does the JVM handle deep call stacks?

ANSWER
A stack overflow occurs when the call stack exceeds its allocated memory. Each recursion call pushes a new frame (containing parameters, return address, local variables) onto the stack. The JVM has a fixed default stack size (~1024 KB per thread). If recursion depth exceeds ~10,000-20,000 frames (depending on frame size), the JVM throws StackOverflowError. You can increase the stack size with -Xss flag, but that's a band-aid. The proper fix is to limit recursion depth or convert to iteration. The JVM does not do tail-call optimization automatically.
FAQ · 5 QUESTIONS

Frequently Asked Questions

01
How do I calculate the time complexity of a recursive function?
02
What is 'Pruning' in the context of backtracking?
03
When is recursion better than iteration in an interview?
04
Can all recursive solutions be converted to iterative ones?
05
What is a 'Trampoline' in Java and why would I use it?
🔥

That's Coding Patterns. Mark it forged?

4 min read · try the examples if you haven't

Previous
Divide and Conquer Problems
15 / 17 · Coding Patterns
Next
Topological Sort Interview Problems