Recursion and backtracking in C#
Problems to practice
- Permutations
- Subsets
- Combination Sum
- N-Queens
- Sudoku
- Fibonacci
Example: permutations
public void Backtrack(int[] nums, List<int> path, List<List<int>> result) {
if (path.Count == nums.Length) {
result.Add(new List<int>(path));
return;
}
for (int i = 0; i < nums.Length; i++) {
if (path.Contains(nums[i])) continue;
path.Add(nums[i]);
Backtrack(nums, path, result);
path.RemoveAt(path.Count - 1);
}
}Interview guidance
Backtracking explores choices recursively and removes the last choice when a path fails.
Can you explain it without the notes?
Recursion and backtracking in C#
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 17_DSA_DOTNET.md · “6. Recursion and backtracking in C#”. Examples are study fragments; framework behavior can depend on your version.