Backtracking Introduction Java | Fresher Interview
Fast recall
Quick revision
Core answer
Whether you can articulate the choose/explore/unchoose loop and pruning, versus generic recursion
Treating backtracking as 'just recursion', or forgetting to undo the choice after exploring
Emphasize the un-choose (state restoration) step and pruning as what makes it backtracking
Interview-ready guide
Interview answer
All backtracking is recursion, but not all recursion is backtracking, and the difference is what makes this question interesting.
Plain recursion typically breaks a problem into subproblems, solves them, and combines the results, like computing factorial or Fibonacci. Backtracking is a specific style of recursion where I build up a candidate solution one choice at a time, and the key idea is that the moment a partial candidate can't possibly lead to a valid complete solution, I abandon it and undo my last choice.
The defining pattern is choose, explore, un-choose. I make a choice, I recurse to explore where that choice leads, and then critically I undo the choice, restoring the state so I can try the next option from the same point.
That undo step, restoring state after exploring, is the single thing that most distinguishes backtracking from plain recursion, and it's also the thing freshers most often forget, which corrupts the results for the sibling branches. The other distinguishing feature is pruning: before I even explore a branch, I check whether it can lead anywhere valid, and if not, I skip it entirely.
Pruning is what keeps backtracking from blindly exploring an exponential search space. Conceptually, I'm walking a decision tree of all possible candidates, going deep, and retreating whenever I hit a dead end. This is the pattern behind permutations, subsets, combinations, N-Queens, Sudoku solvers, and word search.
One last tip: when I record a solution, I store a copy of it, not the mutable working list, otherwise later undo operations will mutate what I already saved.
Backtracking Introduction Java | Fresher Interview
Direct Answer
Backtracking is recursion that builds a candidate step by step and undoes (backtracks) a choice when it can't lead to a valid solution. The extra ingredients over plain recursion are choose -> explore -> un-choose and pruning (abandoning dead branches early). It explores the decision tree of all possibilities, making it ideal for permutations, subsets, N-Queens, and constraint problems.
Code Example
void backtrack(State state, List<Result> results) {
if (isComplete(state)) { // found a full solution
results.add(snapshot(state));
return;
}
for (Choice choice : choices(state)) {
if (!isValid(choice, state)) continue; // PRUNE invalid branches
apply(choice, state); // 1. CHOOSE
backtrack(state, results); // 2. EXPLORE
undo(choice, state); // 3. UN-CHOOSE (restore!)
}
}
// The undo() after exploring is what makes this backtracking, not plain recursion.
Common Mistakes & Pitfalls
- Forgetting to un-choose: not restoring state after the recursive call corrupts later branches.
- No pruning: exploring obviously-invalid branches blows up the runtime.
- Adding by reference: store a copy (snapshot) of the solution, not the mutable working list.
- Calling it plain recursion: the undo + pruning are the distinguishing features.
Check your understanding
Answer these without looking back at the article.
- How do you generate all subsets using backtracking?
- What is the N-Queens problem?
- How do you solve Sudoku with backtracking?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.