Backtracking — State Restoration Bugs That Break Solvers
14 solutions instead of 92? A missing array reset after backtracking corrupts queen positions.
20+ years shipping performance-critical code where algorithms decide the bill. Written from production experience, not tutorials.
- ✓Solid grasp of fundamentals
- ✓Comfortable reading code examples
- ✓Basic production concepts
- Backtracking builds solutions incrementally and abandons branches that can't lead to a valid result — pruning makes it faster than brute force
- Core components: incremental building, feasibility check, explicit undo
- Performance insight: pruning can reduce search space from exponential to feasible (N-Queens 4M → 15K nodes)
- Production insight: missing or buggy pruning causes hidden exponential blowup — your app slows to a crawl without an obvious error
- Biggest mistake: forgetting to restore state after recursion — corrupts all subsequent branches, results are silently wrong
Backtracking is a systematic trial-and-error algorithm for solving constraint satisfaction problems by incrementally building candidates and abandoning them ("backtracking") as soon as you detect they cannot lead to a valid solution. It's the difference between checking every possible combination of queens on a chessboard (brute force, ~4.4 billion for 8×8) and placing queens one row at a time, killing branches the moment a conflict appears (N-Queens solved in ~15,000 partial placements).
The core mechanism is depth-first search over a state space tree, where each node represents a partial solution and edges represent choices. You recurse down, and if you hit a dead end or a complete solution, you unwind the stack and try the next option — this is where state restoration bugs bite you: forget to undo a board modification or a visited flag, and your solver silently skips valid solutions or explores impossible ones.
Backtracking shines for problems like Sudoku, graph coloring, Hamiltonian paths, and combinatorial optimization where the search space is large but constraints are tight enough to prune aggressively. It fails hard when the branching factor is huge and constraints are weak — think brute-forcing a 100-item knapsack (2^100 branches) or solving general SAT instances without heuristics.
In practice, you reach for backtracking when you need exact solutions for NP-complete problems with small-to-medium input sizes (N ≤ 30 for permutations, N ≤ 100 for subsets with good pruning), and you'd use constraint programming (e.g., OR-Tools, MiniZinc) or SAT solvers (e.g., Z3, Glucose) for larger instances. The key insight: backtracking is just DFS with pruning — your performance depends entirely on how early and how often you can say "this branch is hopeless.
Imagine you're navigating a maze. You walk down a corridor and hit a dead end — so you turn around, go back to the last fork, and try a different path. That 'turn around and try again' move is exactly what backtracking is. It's a systematic way of exploring all possible options by trying a choice, checking if it still makes sense, and undoing it if it doesn't — rather than blindly trying every combination from scratch.
Most real-world problems don't come with an obvious answer. Finding all valid ways to place chess queens on a board, generating every possible password combination, or solving a Sudoku puzzle — these all share one thing: there are many possible paths, and most of them are dead ends. Backtracking is the algorithmic strategy that lets you explore those paths efficiently without manually tracking which ones you've already failed at.
In this guide, we'll break down exactly what Backtracking is, why it was designed this way, and how to use it correctly in real projects by implementing production-grade search algorithms.
By the end you'll have both the conceptual understanding and practical code examples to use Backtracking with confidence.
What Backtracking Actually Does (and Why Brute Force Isn't Enough)
Brute force tries every possible combination regardless of whether it's already heading toward failure. Backtracking is smarter — it builds a solution incrementally, and the moment a partial solution violates a constraint, it stops pursuing that branch entirely. This is called 'pruning.'
Think of a decision tree. Every node is a choice. Brute force visits every single node. Backtracking visits a node, checks 'can this possibly lead to a valid answer?', and if the answer is no, it skips the entire subtree rooted there. That's the efficiency win.
The core loop is always the same: choose a candidate, place it, recurse deeper, then unchoose (undo the placement). That 'unchoose' step is what makes backtracking different from plain recursion — you're restoring state so the next candidate gets a clean slate to work with.
This pattern works beautifully for constraint satisfaction problems: puzzles, permutations, combinations, and graph coloring — anywhere the solution space is large but constraints filter most of it out early.
package io.thecodeforge.algorithm; import java.util.ArrayList; import java.util.List; import java.util.Arrays; /** * io.thecodeforge implementation for generating power sets. */ public class SubsetGenerator { public static void main(String[] args) { int[] numbers = {1, 2, 3}; List<List<Integer>> allSubsets = new ArrayList<>(); generateSubsets(numbers, 0, new ArrayList<>(), allSubsets); System.out.println("All subsets of " + Arrays.toString(numbers) + ":"); allSubsets.forEach(System.out::println); System.out.println("Total subsets: " + allSubsets.size()); } private static void generateSubsets( int[] numbers, int startIndex, List<Integer> currentSubset, List<List<Integer>> results) { // Record a snapshot — O(N) copy results.add(new ArrayList<>(currentSubset)); for (int i = startIndex; i < numbers.length; i++) { // CHOOSE currentSubset.add(numbers[i]); // EXPLORE generateSubsets(numbers, i + 1, currentSubset, results); // UNCHOOSE (Backtrack) currentSubset.remove(currentSubset.size() - 1); } } }
Backtracking in C++: N-Queens Example
Backtracking is language-agnostic, but each language has its own idiomatic patterns for handling state. In C++, we typically use vectors and pass-by-reference to manage the board. The core logic remains identical: for each column, try each row, check safety, place queen, recurse, then backtrack (restore row to -1).
Here's the same N-Queens solver we showed in Java, now in C++. Notice how the feasibility check uses the same column-by-column approach, and the undo step is simply resetting the board cell to -1. C++ gives us fine-grained control over memory, but the backtracking pattern is unchanged.
This example also demonstrates the use of a recursive lambda (C++14 and later) or a separate function. We'll use a compact recursive function.
Compile with g++ -std=c++17 NQueens.cpp -o NQueens and run.
The output should match the Java version: 2 solutions for a 4×4 board.
#include <iostream> #include <vector> #include <cmath> using namespace std; void solveNQueens(int n, int col, vector<int>& queenInRow, vector<vector<int>>& solutions) { if (col == n) { solutions.push_back(queenInRow); // copy of current state return; } for (int row = 0; row < n; ++row) { if (isPlacementSafe(queenInRow, col, row)) { queenInRow[col] = row; // CHOOSE solveNQueens(n, col + 1, queenInRow, solutions); // EXPLORE // UNCHOOSE is implicit via row overwrite } } } bool isPlacementSafe(const vector<int>& queenInRow, int col, int row) { for (int prevCol = 0; prevCol < col; ++prevCol) { int prevRow = queenInRow[prevCol]; if (prevRow == row || abs(prevRow - row) == abs(prevCol - col)) { return false; // PRUNE } } return true; } void printBoard(const vector<int>& queenInRow, int n) { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cout << (queenInRow[j] == i ? " Q " : " . "); } cout << endl; } cout << endl; } int main() { int n = 4; vector<int> queenInRow(n, -1); vector<vector<int>> solutions; solveNQueens(n, 0, queenInRow, solutions); cout << "Found " << solutions.size() << " solutions for " << n << "-Queens." << endl; for (auto& sol : solutions) { printBoard(sol, n); } return 0; }
solutions.push_back(queenInRow) copies the entire vector. That's safe and intentional. If you accidentally store a reference or pointer to the working vector, all entries will be identical and corrupted. Same golden rule applies.std::vector for state and passing by reference to avoid deep copies during recursion. But always copy when storing a result. The performance gain from reference-passing is significant for large boards.State Space Tree: Visualizing the Search Process
A state space tree is a visual representation of all possible states explored by backtracking. Each node represents a partial solution, and each branch represents a choice. The root is the empty state. Leaves are either complete solutions (recorded) or dead ends (pruned due to constraint violation).
For a 4-Queens board, the tree starts empty. At level 0 (column 0), we try rows 0-3. Placing a queen at row 0 leads to a subtree; placing at row 1 leads to another, etc. At level 1 (column 1), we try rows 0-3 again but prune those that conflict with the existing queens. The tree grows until we either reach a complete placement (solution) or find no valid rows, which becomes a dead end branch.
The diagram below shows a simplified state space tree for the first few levels of 4-Queens. The pruned branches are marked with an X. This visualization helps understand how backtracking explores only a fraction of the total possibilities.
For larger N, the tree becomes dense but pruned heavily. The key insight: the earlier a constraint is violated, the deeper the cut in the tree, saving enormous effort.
Pruning: The Feature That Makes Backtracking Fast
Generating all subsets is pure exploration — there are no invalid paths to prune. But backtracking really earns its keep when constraints let you eliminate huge branches early.
The N-Queens problem is the classic demonstration. Place N queens on an N×N chessboard so that no two queens attack each other (same row, column, or diagonal). A brute force approach would try all N^N placements. With pruning, the moment placing a queen creates a conflict, you stop and backtrack — you never explore any of the millions of board states that would follow from that illegal placement.
For an 8×8 board, brute force checks 16 million+ configurations. Backtracking with pruning checks roughly 15,000. That's the power of cutting entire subtrees.
The constraint check — the 'is this placement still valid?' question — is called the bounding function or feasibility check. Writing a tight, fast feasibility check is the single biggest lever you have for making backtracking solutions performant.
package io.thecodeforge.algorithm; import java.util.ArrayList; import java.util.List; public class NQueensSolver { public static void main(String[] args) { int n = 4; List<int[]> solutions = new ArrayList<>(); solveNQueens(n, 0, new int[n], solutions); System.out.println("Found " + solutions.size() + " solutions for " + n + "-Queens."); solutions.forEach(sol -> printBoard(sol, n)); } private static void solveNQueens(int n, int col, int[] queenInRow, List<int[]> solutions) { if (col == n) { solutions.add(queenInRow.clone()); return; } for (int row = 0; row < n; row++) { if (isPlacementSafe(queenInRow, col, row)) { queenInRow[col] = row; // CHOOSE solveNQueens(n, col + 1, queenInRow, solutions); // EXPLORE // UNCHOOSE is implicit via row overwrite } } } private static boolean isPlacementSafe(int[] queenInRow, int col, int row) { for (int prevCol = 0; prevCol < col; prevCol++) { int prevRow = queenInRow[prevCol]; if (prevRow == row || Math.abs(prevRow - row) == Math.abs(prevCol - col)) { return false; // PRUNE branch } } return true; } private static void printBoard(int[] queenInRow, int n) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { System.out.print(queenInRow[j] == i ? " Q " : " . "); } System.out.println(); } System.out.println(); } }
Recognizing When Backtracking Is the Right Tool
Backtracking isn't always the right choice. It's best when the problem has all three of these properties:
- You need all solutions (or need to verify one exists), not just a single optimal one.
- The solution is built incrementally — you make a series of choices.
- Constraints can eliminate branches early — otherwise, you're just doing brute force.
Classic backtracking problems: N-Queens, Sudoku, generating permutations/combinations/subsets, word search on a grid, graph coloring, and the rat-in-a-maze problem.
Not a great fit for backtracking: finding the shortest path (use BFS/Dijkstra), problems with overlapping subproblems (use DP), or cases where you need statistical probability across all paths (use DP again).
package io.thecodeforge.algorithm; import java.util.ArrayList; import java.util.List; public class PermutationGenerator { public static void main(String[] args) { String input = "ABC"; List<String> results = new ArrayList<>(); boolean[] used = new boolean[input.length()]; backtrack(input.toCharArray(), used, new StringBuilder(), results); results.forEach(System.out::println); } private static void backtrack(char[] chars, boolean[] used, StringBuilder sb, List<String> results) { if (sb.length() == chars.length) { results.add(sb.toString()); return; } for (int i = 0; i < chars.length; i++) { if (used[i]) continue; // PRUNE used[i] = true; // CHOOSE sb.append(chars[i]); backtrack(chars, used, sb, results); // EXPLORE sb.deleteCharAt(sb.length() - 1); // UNCHOOSE used[i] = false; } } }
currentPermutation.toString() to snapshot the result, not just add the StringBuilder directly. If you added the StringBuilder object itself, every result in your list would point to the same object — and every subsequent modification would corrupt all previously stored results. Always snapshot mutable state before storing it.When NOT to Use Backtracking
Backtracking is not a silver bullet. There are clear cases where it's the wrong tool, and knowing them will save you from wasted hours and poor performance.
1. Problems with no constraints to prune – If every branch is equally valid and you still need to explore all possibilities, backtracking is identical to brute force. Examples: generating all subsets of a set (no constraints), generating all permutations of a string with no duplicates. In these cases, the simple iterative or recursive approach is fine — backtracking adds complexity without benefit.
2. Problems where you only need one (or a few) solutions – Backtracking is designed to find all valid completions. If you just need to confirm existence or find the first solution, use a simpler search like DFS with early exit, or a greedy heuristic. For instance, solving a 9×9 Sudoku: backtracking finds one solution quickly, but if you want partial progress, constraint propagation alone may suffice.
3. Problems with overlapping subproblems – If the same partial state is reached via different paths, backtracking will recompute it each time. Dynamic programming (memoization) is exponentially faster. Classic example: Fibonacci numbers, where backtracking (literally trying all 2^n choices) is ridiculous; DP gives O(n).
4. Problems with a large branching factor and shallow depth – If you have many choices at each step but few steps to completion, the state space is wide but shallow. Backtracking still explores every branch, which can be enormous if the feasibility check doesn't prune heavily. Example: enumerating all possible assignments for a small scheduling problem with 30 options per slot and 5 slots: 30^5 = 24 million possibilities, and pruning may be weak.
5. Problems where the constraints are expensive to evaluate – If the feasibility check is O(n^2) or worse, and you're calling it at every node, the overhead can dwarf the benefit of pruning. In such cases, try to precompute or reduce the check's complexity.
Always profile before committing to backtracking — if a simpler solution exists, use it.
Backtracking vs Dynamic Programming vs Brute Force
Many engineers confuse backtracking with dynamic programming or brute force. They're cousins, but they handle constraints and overlapping work differently.
Brute force tries every combination with no pruning. It's simple but always exponential.
Backtracking prunes branches that can't lead to a valid solution. It's still worst-case exponential, but in practice it's much faster.
Dynamic programming caches results of overlapping subproblems to avoid recomputation. It works when the problem has optimal substructure and overlapping subproblems — things like Fibonacci, knapsack, edit distance.
The key difference: backtracking explores a tree and cuts dead branches. DP memoizes to avoid revisiting the same branch. When subproblems overlap heavily, DP wins. When the search space is large but most paths are quickly invalid, backtracking wins.
In some problems (like the Knight's Tour), both can be used — backtracking with a heuristic like Warnsdorff's rule is typically faster than DP because the state space of visited cells is hard to cache.
package io.thecodeforge.algorithm; import java.util.*; public class KnightTour { private static final int[] dx = {2, 1, -1, -2, -2, -1, 1, 2}; private static final int[] dy = {1, 2, 2, 1, -1, -2, -2, -1}; public static boolean solveKnightTour(int n) { int[][] board = new int[n][n]; // Fill with -1 to mark unvisited for (int[] row : board) Arrays.fill(row, -1); board[0][0] = 0; if (backtrack(board, 0, 0, 1, n)) { printBoard(board); return true; } System.out.println("No solution."); return false; } private static boolean backtrack(int[][] board, int x, int y, int step, int n) { if (step == n * n) return true; // all cells visited // Warnsdorff's heuristic: order moves by number of onward moves List<Move> moves = new ArrayList<>(); for (int i = 0; i < 8; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if (nx >= 0 && nx < n && ny >= 0 && ny < n && board[nx][ny] == -1) { int degree = countOnwardMoves(board, nx, ny, n); moves.add(new Move(nx, ny, degree)); } } moves.sort(Comparator.comparingInt(m -> m.degree)); for (Move m : moves) { board[m.x][m.y] = step; if (backtrack(board, m.x, m.y, step + 1, n)) return true; board[m.x][m.y] = -1; // undo } return false; } private static int countOnwardMoves(int[][] board, int x, int y, int n) { int count = 0; for (int i = 0; i < 8; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if (nx >= 0 && nx < n && ny >= 0 && ny < n && board[nx][ny] == -1) count++; } return count; } private static void printBoard(int[][] board) { for (int[] row : board) { for (int cell : row) System.out.printf("%2d ", cell); System.out.println(); } } static class Move { int x, y, degree; Move(int x, int y, int degree) { this.x = x; this.y = y; this.degree = degree; } } }
Backtracking vs Recursion: Understanding the Difference
Although backtracking is often implemented with recursion, they are not the same thing. Recursion is a programming technique where a function calls itself to solve smaller instances of the same problem. Backtracking is an algorithmic strategy that systematically explores candidate solutions and abandons those that cannot satisfy constraints.
You can have recursion without backtracking (e.g., a recursive factorial function) and backtracking without explicit recursion (e.g., using an explicit stack). The key distinction is the undo step: backtracking always includes state restoration after recursion, while plain recursion may just compute and return a value without side effects.
Another difference: backtracking always explores a decision tree with pruning; recursion can be used for problems that don't involve choices (e.g., tree traversal).
The table below summarizes the contrasts.
// Example: Recursive factorial (no backtracking) public class Factorial { public static int factorial(int n) { if (n <= 1) return 1; return n * factorial(n - 1); } } // Example: Backtracking subset generation (includes undo) public class Subsets { void backtrack(int[] nums, int start, List<Integer> cur, List<List<Integer>> res) { res.add(new ArrayList<>(cur)); for (int i = start; i < nums.length; i++) { cur.add(nums[i]); backtrack(nums, i + 1, cur, res); cur.remove(cur.size() - 1); // UNDO } } }
Optimizing Backtracking: Heuristics, Ordering and Early Termination
Even with good pruning, backtracking can still be slow. Three optimisation techniques can drastically reduce runtime:
1. Variable Ordering (Most Constraining First) Choose the next variable with the most constraints (e.g., the cell with fewest remaining values in Sudoku). This minimises branching factor early.
2. Value Ordering (Least Constraining Value) Try values that are least likely to cause future conflicts. Warnsdorff's rule for Knight's Tour is a classic example — it reduces backtracks by up to 90%.
3. Forward Checking After making a choice, propagate constraints by eliminating values from future possibilities. If any future variable has no remaining valid values, prune immediately.
Forward checking is more code but often pays off for problems like Sudoku or graph coloring. Without it, you discover a dead end only after recursing deeper.
These techniques are common in constraint satisfaction problems (CSPs) and are the difference between a backtracking solver that finishes in seconds vs one that never completes.
package io.thecodeforge.algorithm; public class SudokuSolver { private static final int SIZE = 9; private static final int BOX = 3; public boolean solve(int[][] board) { int[] empty = findEmpty(board); if (empty == null) return true; // solved int row = empty[0], col = empty[1]; for (int num = 1; num <= SIZE; num++) { if (isValid(board, row, col, num)) { board[row][col] = num; if (solve(board)) return true; board[row][col] = 0; // undo } } return false; } private int[] findEmpty(int[][] board) { // Most constrained first: pick cell with fewest possibilities int minCount = SIZE + 1; int[] best = null; for (int r = 0; r < SIZE; r++) { for (int c = 0; c < SIZE; c++) { if (board[r][c] == 0) { int count = countValid(board, r, c); if (count < minCount) { minCount = count; best = new int[]{r, c}; } } } } return best; } private int countValid(int[][] board, int row, int col) { int count = 0; for (int num = 1; num <= SIZE; num++) { if (isValid(board, row, col, num)) count++; } return count; } private boolean isValid(int[][] board, int row, int col, int num) { for (int c = 0; c < SIZE; c++) if (board[row][c] == num) return false; for (int r = 0; r < SIZE; r++) if (board[r][col] == num) return false; int boxRow = row - row % BOX; int boxCol = col - col % BOX; for (int r = boxRow; r < boxRow + BOX; r++) for (int c = boxCol; c < boxCol + BOX; c++) if (board[r][c] == num) return false; return true; } }
- After placing a queen, eliminate that row and both diagonals from future rows.
- In Sudoku, after entering a number, remove it from candidates in the same row, column, and box.
- If any variable ends up with zero candidates, the current branch cannot lead to a solution — prune immediately.
Practice Problems: Solidify Your Backtracking Skills
The best way to master backtracking is to implement it across different problem types. Below are seven classic problems that cover the key patterns: subset generation, permutation, constraint satisfaction, and pathfinding. Each link leads to a detailed explanation with code on TheCodeForge.
- Rat in a Maze – A classic pathfinding backtracking problem. Find all paths from top-left to bottom-right in a grid with obstacles. Demonstrates grid-state mutation and restoration.
- [Solve Rat in a Maze →](https://thecodeforge.io/backtracking/rat-in-a-maze)
- N-Queens Problem – Place N queens on an N×N board without conflicts. The quintessential backtracking problem. We covered it in depth above.
- [Solve N-Queens →](https://thecodeforge.io/backtracking/n-queens)
- Word Break Problem – Given a string and a dictionary, determine if the string can be segmented into dictionary words. Backtracking with pruning can solve it, though DP is more efficient for large inputs.
- [Solve Word Break →](https://thecodeforge.io/backtracking/word-break)
- Subsets (Power Set) – Generate all subsets of a given set. The simplest backtracking pattern – great for understanding the choose-explore-unchoose loop.
- [Solve Subsets →](https://thecodeforge.io/backtracking/subsets)
- Permutations – Generate all arrangements of a set of elements. Introduces the 'used' array pattern for avoiding repeats.
- [Solve Permutations →](https://thecodeforge.io/backtracking/permutations)
- Sudoku Solver – Fill a partially completed 9×9 grid so that every row, column, and 3×3 box contains digits 1-9. Demonstrates constraint propagation and forward checking.
- [Solve Sudoku →](https://thecodeforge.io/backtracking/sudoku-solver)
- M-Coloring Problem – Color a graph using at most M colors such that no two adjacent vertices share the same color. Classic constraint satisfaction problem.
- [Solve M-Coloring →](https://thecodeforge.io/backtracking/m-coloring)
Start with Subsets and Permutations for the fundamentals, then move to N-Queens and Rat in a Maze for constraint-based problems. Finally, tackle Sudoku and M-Coloring for advanced pruning and ordering techniques.
How Backtracking Actually Works: The Trial-and-Terror Factory
Forget the textbook definitions. Here's what really happens: you build a solution piece by piece, and the moment you realize the current path leads nowhere, you rip it out and try something else. That's it. Choose, explore, check, backtrack, repeat.
The N-Queens problem is the perfect specimen. You drop a queen on row 1, column 1. Move to row 2. Can't place one? Backtrack to row 1, shift the queen to column 2. Keep going. The algorithm doesn't know if a placement is final — it commits tentatively, tests the next level, and if the universe screams "conflict," it undoes its last move. This isn't magic. It's recursion with a rollback button.
Every backtracking algorithm follows the same rhythm: make a choice, recurse, validate, undo. The undo step is what separates it from brute force. Brute force generates every combo upfront. Backtracking stops the moment a partial solution is impossible. That's the performance win.
// io.thecodeforge — dsa tutorial public class NQueensDriver { private static final int BOARD_SIZE = 4; public static void main(String[] args) { int[][] board = new int[BOARD_SIZE][BOARD_SIZE]; if (solveNQueens(board, 0)) { printBoard(board); } else { System.out.println("No solution exists."); } } private static boolean solveNQueens(int[][] board, int row) { if (row == BOARD_SIZE) return true; for (int col = 0; col < BOARD_SIZE; col++) { if (isSafe(board, row, col)) { board[row][col] = 1; // choose if (solveNQueens(board, row + 1)) return true; // explore board[row][col] = 0; // backtrack (undo) } } return false; } private static boolean isSafe(int[][] board, int row, int col) { for (int i = 0; i < row; i++) if (board[i][col] == 1) return false; for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) if (board[i][j] == 1) return false; for (int i = row - 1, j = col + 1; i >= 0 && j < BOARD_SIZE; i--, j++) if (board[i][j] == 1) return false; return true; } private static void printBoard(int[][] board) { for (int[] row : board) { for (int cell : row) System.out.print(cell == 1 ? "Q " : ". "); System.out.println(); } } }
Standard Backtracking Problems: The Rogue's Gallery
If you've spent any time on LeetCode or in an interview loop, you've seen the same patterns over and over. N-Queens, Sudoku Solver, Permutations, Subsets, Combination Sum, the Knight's Tour. They all scream "backtracking" — and for good reason.
Here's the litmus test: does the problem ask you to find all solutions, or just one? Does it involve making a sequence of choices where each choice restricts future ones? Is there a clear way to check if a partial solution is still viable? If yes, you're in backtracking territory.
Sudoku is a textbook case. You try placing a digit. Check row, column, and 3x3 box. If valid, recurse to the next empty cell. If you hit a dead end, erase the digit and try the next one. No shortcuts. No heuristics. Just systematic trial with immediate validation.
Permutations? Same deal. Swap elements, recurse, swap back. The undo is the swap-back. Without it, you'd be permuting in place and corrupting your own state. Every backtracking problem has this DNA: a loop over candidates, a validity check, a recursive call, and a cleanup step.
// io.thecodeforge — dsa tutorial public class SudokuSolver { public boolean solveSudoku(char[][] board) { for (int row = 0; row < 9; row++) { for (int col = 0; col < 9; col++) { if (board[row][col] == '.') { for (char num = '1'; num <= '9'; num++) { if (isValid(board, row, col, num)) { board[row][col] = num; // choose if (solveSudoku(board)) return true; // explore board[row][col] = '.'; // backtrack } } return false; // no valid digit for this cell } } } return true; } private boolean isValid(char[][] board, int row, int col, char num) { for (int i = 0; i < 9; i++) { if (board[row][i] == num) return false; if (board[i][col] == num) return false; int boxRow = 3 * (row / 3) + i / 3; int boxCol = 3 * (col / 3) + i % 3; if (board[boxRow][boxCol] == num) return false; } return true; } }
The Phantom Queen: How Missing State Restoration Crashed an N-Queens System
- Every 'add' must have a matching 'remove' in backtracking — state restoration is not optional.
- Testing the feasibility function alone doesn't catch state corruption bugs; test the full solver with small known boards first.
- Use immutable snapshots when storing solutions, but mutable state internally — just ensure you restore it correctly.
System.out.println("Before explore: " + currentList);System.out.println("After explore: " + currentList);results.add(currentList) with results.add(new ArrayList<>(currentList))if (depth > 100) throw new RuntimeException("Too deep");System.out.println("Depth: " + depth + " choices at this level: " + candidates.size());long start = System.nanoTime(); boolean safe = isSafe(...); long elapsed = System.nanoTime() - start; if (elapsed > 1_000_000) System.out.println("Slow check: " + elapsed);Add early termination: if a placement is invalid, don't even call feasibility for dependent placements.| Aspect | Backtracking | Brute Force | Dynamic Programming |
|---|---|---|---|
| Explores invalid branches? | No — prunes them early | Yes — tries everything | Doesn't explore — uses cached results |
| Time complexity | Depends on pruning quality | Always worst-case O(N!) or O(2^N) | O(N) to O(N^2) on subproblem count |
| Memory usage | O(depth of recursion) | O(total candidates) if all stored | O(number of distinct subproblems) |
| Best for | Constraint satisfaction, all-solutions problems | Tiny input, no constraints | Overlapping subproblems, optimal substructure |
| Needs feasibility check? | Yes — core to its efficiency | No | No — but need recurrence relation |
| Typical problems | N-Queens, Sudoku, permutations | Password cracking (no constraints) | Fibonacci, knapsack, edit distance |
| Difficulty to implement | Moderate — state management is tricky | Simple — nested loops | Moderate — recurrence and memoization |
| File | Command / Code | Purpose |
|---|---|---|
| SubsetGenerator.java | /** | What Backtracking Actually Does (and Why Brute Force Isn't E |
| NQueens.cpp | using namespace std; | Backtracking in C++ |
| NQueensSolver.java | public class NQueensSolver { | Pruning |
| PermutationGenerator.java | public class PermutationGenerator { | Recognizing When Backtracking Is the Right Tool |
| KnightTour.java | public class KnightTour { | Backtracking vs Dynamic Programming vs Brute Force |
| SimpleRecursion.java | public class Factorial { | Backtracking vs Recursion |
| SudokuSolver.java | public class SudokuSolver { | Optimizing Backtracking |
| NQueensDriver.java | public class NQueensDriver { | How Backtracking Actually Works |
Key takeaways
Common mistakes to avoid
4 patternsForgetting to undo state changes
Storing a reference instead of a snapshot
results.add(new ArrayList<>(currentList)) or results.add(currentString.toString()). Never add the live mutable object itself.Writing an incorrect or over-eager feasibility check
Using backtracking for problems that don't need all solutions
Practice These on LeetCode
Interview Questions on This Topic
What is the difference between backtracking and dynamic programming — when would you choose one over the other?
Walk me through how you'd solve Sudoku using backtracking. What does your feasibility check look like, and how do you ensure efficient pruning?
If your backtracking solution is too slow for large inputs, what are three concrete techniques you can apply to speed it up without changing the core algorithm?
What is the time complexity of backtracking, and how does pruning affect it?
Frequently Asked Questions
No — recursion is a programming technique (a function calling itself), while backtracking is an algorithmic strategy that uses recursion as its vehicle. The defining feature of backtracking is the explicit undo step: after recursing, you restore state so the next candidate starts clean. Plain recursion doesn't require that.
It depends heavily on the problem and the quality of your pruning. In the worst case (no pruning at all) it's equivalent to brute force — O(N!) for permutations, O(2^N) for subsets. In practice, good pruning can reduce the explored space by orders of magnitude, which is why backtracking is practical for problems where brute force is not.
Your base case defines a 'complete' solution — typically when you've placed all items, filled all positions, or exhausted the input. For problems like subset generation, every state (not just leaf nodes) is a valid result, so you record at every level. For problems like N-Queens, you only record when all N queens are placed without conflict. Understanding exactly what 'done' means for your problem is the first thing to nail down before writing a single line of backtracking code.
Yes, but only if you need to find an optimal solution among all valid ones (e.g., find the permutation that maximizes profit). However, if the problem has optimal substructure (like shortest path), dynamic programming is usually more efficient. Backtracking can be adapted by tracking a best-so-far and pruning branches that can't beat it (branch and bound).
20+ years shipping performance-critical code where algorithms decide the bill. Written from production experience, not tutorials.
That's Greedy & Backtracking. Mark it forged?
9 min read · try the examples if you haven't