Recursion and backtracking in Python
Problems to practice
- Fibonacci
- Factorial
- Permutations
- Subsets
- Combination Sum
- N-Queens
- Sudoku
Example: subset generation
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 resultInterview guidance
Backtracking tries a branch and then undoes it to continue exploring other cases.
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
Turn this concept into a clear answer.
Practice this conceptAdapted from 18_DSA_PYTHON.md · “6. Recursion and backtracking in Python”. Examples are study fragments; framework behavior can depend on your version.