Intermediate·Concept·2 min read

Recursion and backtracking in C#

Practice this question

Problems to practice

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

Example: permutations

csharp
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.


Adapted from 17_DSA_DOTNET.md · “6. Recursion and backtracking in C#”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.