LESSON 67 / 75·2 min readIntermediate

Recursion and backtracking in Python

Should knowConcept lesson
01 Understand02 Recall03 Prepare

Problems to practice

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

Example: subset generation

Python
def subsets(nums):
    result = []

    def backtrack(start, path):
        result.append(path.copy())
        for i in range(start, len(nums)):
            path.append(nums[i])
            backtrack(i + 1, path)
            path.pop()

    backtrack(0, [])
    return result

Interview guidance

Backtracking tries a branch and then undoes it to continue exploring other cases.


PAUSE & RECALL

Can you explain it without the notes?

Recursion and backtracking in Python

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 Python

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