Algorithms10 lessons32 quiz questions
Recursion & Backtracking
Backtracking is a systematic algorithm for generating all solutions to a problem by building candidates incrementally and abandoning (backtracking) candidates that fail constraints. The mental model: you are exploring a decision tree — at each node, you choose one option, recurse into the...
What You Will Learn
- ✓Recursion Fundamentals & Call Stack
- ✓Subsets — Backtracking Template
- ✓Permutations
- ✓Combination Sum & Pruning
- ✓N-Queens & Constraint Satisfaction
- ✓Sudoku Solver
- ✓Word Search & Grid Backtracking
- ✓Generate Parentheses & String Backtracking
- ✓Advanced Backtracking
- ✓Mock Interview & Pattern Review
Overview
Backtracking is a systematic algorithm for generating all solutions to a problem by building candidates incrementally and abandoning (backtracking) candidates that fail constraints. The mental model: you are exploring a decision tree — at each node, you choose one option, recurse into the subtree, then undo the choice and try the next option. The template is always: choose → explore → un-choose. The key insight: backtracking is exhaustive DFS with pruning. Pruning is what makes backtracking practical.
Understanding Stacks
A stack is a LIFO (Last In, First Out) data structure. Think of a stack of plates — you can only add or remove from the top. Stacks are used for tracking state, backtracking, and expression evaluation.
Why stacks are interview favorites:
They model function call stacks (recursion)
They solve parentheses matching, expression evaluation
Monotonic stacks solve "next greater/smaller" problems efficiently
Key patterns:
Matching pairs: parentheses, brackets, HTML tags
Monotonic stack: maintain increasing/decreasing order to find next greater/smaller element in O(n)
Expression evaluation: convert infix to postfix, evaluate postfix
Backtracking: undo operations (browser history, undo/redo)
When to use a stack:
"Find the nearest/next greater/smaller element" → Monotonic stack
"Check if balanced/valid" → Regular stack
"Evaluate expression" → Two stacks (operators operands)
Recursion & Backtracking
Backtracking systematically explores all possibilities by making choices, exploring, and undoing (backtracking) when a path doesn't work.
The Template
Subsets
Permutations
N-Queens
Key Problems
#78 Subsets, #46 Permutations, #39 Combination Sum
#51 N-Queens, #79 Word Search, #37 Sudoku Solver
Interview Tip
"Backtracking is DFS with pruning. The key is knowing WHEN to prune — every valid pruning condition dramatically reduces the search space."
Java Implementation:
Sample Quiz Questions
1. What is the key difference between permutations and combinations in backtracking?
Understand·Difficulty: 2/5
2. In backtracking, why must you push `[...path]` instead of `path` to the result?
Understand·Difficulty: 2/5
3. Time complexity of generating all permutations of n distinct elements?
Remember·Difficulty: 2/5
+ 29 more questions available in the full app.
Related Topics
Master Recursion & Backtracking for Your Next Interview
Get access to full lessons, adaptive quizzes, cheat sheets, code playground, and progress tracking — completely free.