LESSON 39 / 75·2 min readIntermediate

Recursion and backtracking in Java

Should knowConcept lesson
01 Understand02 Recall03 Prepare

Problems to practice

  • Fibonacci
  • Factorial
  • Permutations
  • Subsets
  • N-Queens
  • Sudoku
  • Combination Sum

Example: subset generation

java
public void backtrack(int[] nums, int start, List<Integer> path, List<List<Integer>> result) {
    result.add(new ArrayList<>(path));

    for (int i = start; i < nums.length; i++) {
        path.add(nums[i]);
        backtrack(nums, i + 1, path, result);
        path.remove(path.size() - 1);
    }
}

Interview guidance

Base case is critical. Backtracking means explore a choice, then undo it.


PAUSE & RECALL

Can you explain it without the notes?

Recursion and backtracking in Java

Use your own words. Explain an example and one limitation before checking the explanation.

Check your explanation

Common questions

CONNECT IT TO YOUR INTERVIEW

Turn this concept into a clear answer.

Practice this concept
YOUR NEXT STEP

Keep connecting the ideas.

Next lesson: Trees and BST in Java

Adapted from 15_DSA_JAVA.md · “6. Recursion and backtracking in Java”. Examples are study fragments; framework behavior can depend on your version.