What is Recursion?
Recursion is when a function calls itself to solve smaller instances of the same problem. Every recursive solution has a base case (termination) and a recursive case (breakdown).Quick Recognition: Problems that can be broken into smaller self-similar subproblems. Look for: tree/graph traversal, nested structures, divide and conquer.
Pattern Recognition Checklist
Use Recursion When
- Tree/graph traversal (DFS)
- Nested or hierarchical structures
- Divide and conquer algorithms
- Backtracking problems
- Mathematical sequences (factorial, fibonacci)
- Subproblems have same structure as original
Don't Use When
- Simple iteration suffices
- Large input (stack overflow risk)
- No natural subproblem structure
- Tail recursion can be converted to loop
Recursion Types
| Type | Description | Example |
|---|---|---|
| Linear | One recursive call | Factorial, sum of array |
| Binary | Two recursive calls | Fibonacci, binary tree |
| Multiple | Many recursive calls | N-ary tree traversal |
| Mutual | Functions call each other | Even/odd check |
| Nested | Recursive call in argument | Ackermann function |
| Tail | Recursive call is last operation | Optimizable to iteration |
The Three Laws of Recursion
- A recursive function must have a base case
- A recursive function must change state toward the base case
- A recursive function must call itself
When to Use
Tree/Graph Traversal
DFS, preorder, inorder, postorder
Divide and Conquer
Merge sort, quick sort
Backtracking
Permutations, combinations
Dynamic Programming
Top-down memoization
The Recursive Template
Pattern Variations
1. Simple Recursion (Factorial)
2. Tree Recursion (Fibonacci)
3. Tail Recursion
4. Helper Function Pattern
5. Binary Tree Traversal
Classic Problems
| Problem | Pattern | Key Insight |
|---|---|---|
| Factorial | Simple | One recursive call |
| Fibonacci | Tree + Memo | Multiple calls, cache results |
| Tower of Hanoi | Mutual | Move n-1, move 1, move n-1 |
| Tree Traversal | Binary | Left subtree, root, right |
| Permutations | Backtrack | Choose, explore, unchoose |
Common Mistakes
Debugging Recursion
1
Verify Base Case
Is there at least one base case? Does it return correct value?
2
Check Progress
Does each recursive call move toward the base case?
3
Trace Small Input
Manually trace with n=0, n=1, n=2 to verify logic
4
Add Print Statements
Print entry/exit to visualize call stack
5
Consider Memoization
If same subproblems repeat, add caching
Interview Problems by Company
- Easy
- Medium
- Hard
| Problem | Company | Key Concept |
|---|---|---|
| Fibonacci | All | Binary recursion + memo |
| Reverse String | Amazon | Two-pointer recursion |
| Merge Two Lists | All FAANG | Linked list recursion |
| Maximum Depth of Tree | All | Binary tree recursion |
Interview Tips
How to Explain Your Approach
How to Explain Your Approach
Script for interviews:
- “I’ll solve this recursively because it breaks into smaller self-similar subproblems.”
- “My base case is [describe when to stop].”
- “My recursive case [describe how to break down].”
- “I combine results by [describe combination].”
- “Time complexity is O(X) because [reasoning].”
Recursion vs Iteration
Recursion vs Iteration
| Aspect | Recursion | Iteration |
|---|---|---|
| Readability | Often cleaner for trees | Better for simple loops |
| Memory | Stack frames | Usually O(1) |
| Performance | Function call overhead | Generally faster |
| When to choose | Complex branching | Simple linear process |
Converting to Iteration
Converting to Iteration
Many recursive solutions can use explicit stack:
Practice Problems
Pow(x, n)
Fast exponentiation recursion
K-th Symbol
Pattern recognition recursion
Flatten Nested List
Recursive flattening
Decode String
Nested structure recursion
Practice Roadmap
1
Day 1: Basic Recursion
- Solve: Fibonacci, Factorial, Sum of Array
- Focus: Base case + recursive case pattern
2
Day 2: Tree Recursion
- Solve: Max Depth, Same Tree, Invert Tree
- Focus: Binary tree traversal patterns
3
Day 3: Divide and Conquer
- Solve: Pow(x, n), Merge Sort
- Focus: Splitting and combining
4
Day 4: Advanced Recursion
- Solve: Decode String, Regular Expression
- Focus: Complex nested structures