Challenge - 5 Problems
Backtracking Explorer
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
easyWhat is Backtracking?
Which statement best describes the backtracking strategy?
Attempts:
2 left
💡 Hint
Think about what happens when a partial solution fails a constraint — do we keep going or turn back?
✗ Incorrect
Backtracking builds solutions step-by-step. At each step, if the current partial solution violates a constraint, we undo the last choice (backtrack) and try the next available option. This is different from greedy (always picks best local option), divide-and-conquer (independent subproblems), and DP (memoization).
❓ Predict Output
intermediateTrace a Simple Backtracking Function
What does this C# backtracking function print for n=2?
DSA C#
void Solve(int n, List<int> path) { if (path.Count == n) { Console.WriteLine("[" + string.Join(",", path) + "]"); return; } for (int i = 1; i <= n; i++) { if (!path.Contains(i)) { path.Add(i); Solve(n, path); path.RemoveAt(path.Count - 1); // backtrack } } } Solve(2, new List<int>());
Attempts:
2 left
💡 Hint
Trace what path contains at each recursive call. The backtrack line removes the last added element.
✗ Incorrect
n=2: path starts empty. Add 1→path=[1]. Add 2→path=[1,2]→print [1,2]. Backtrack→path=[1]. No more options (2 used). Backtrack→path=[]. Add 2→path=[2]. Add 1→path=[2,1]→print [2,1]. Backtrack. Done. Output: [1,2] then [2,1].
🧠 Conceptual
intermediateDecision Tree Depth
In a backtracking decision tree for generating all permutations of n elements, what is the maximum depth of the tree?
Attempts:
2 left
💡 Hint
Each level of the tree corresponds to choosing one element for the current position in the permutation.
✗ Incorrect
The decision tree has depth n because we make exactly n choices — one element per position in the permutation. At depth 1 we choose the first element, at depth 2 the second, and so on until depth n where the permutation is complete. The number of leaves is n! (total permutations), but the depth is just n.
🔧 Debug
intermediateFind the Missing Backtrack Step
This code is supposed to find all subsets but produces duplicates. What is missing?
DSA C#
void Subsets(int[] nums, int start, List<int> current, List<List<int>> result) { result.Add(new List<int>(current)); for (int i = start; i < nums.Length; i++) { current.Add(nums[i]); Subsets(nums, i + 1, current, result); // BUG: missing something here } }
Attempts:
2 left
💡 Hint
After exploring all subsets that include nums[i], we must undo adding nums[i] before trying nums[i+1].
✗ Incorrect
After the recursive call returns, nums[i] is still in current — it was never removed. The next iteration adds nums[i+1] on top of a list that still contains nums[i], causing duplicates and wrong subsets. The fix is current.RemoveAt(current.Count - 1) after the recursive call — this is the backtrack step that undoes the choice.
🚀 Application
hardPruning in Backtracking
In a backtracking solution for N-Queens, which condition correctly prunes invalid placements before recursing?
DSA C#
bool IsSafe(int[] board, int row, int col) { for (int i = 0; i < row; i++) { if (/* condition */) return false; } return true; }
Attempts:
2 left
💡 Hint
A queen at row i, column board[i] attacks the current (row, col) if they share a column or either diagonal.
✗ Incorrect
Three attack conditions: (1) same column: board[i]==col. (2) same major diagonal (top-left to bottom-right): column difference equals row difference → board[i]-i == col-row. (3) same minor diagonal: board[i]+i == col+row. If any condition holds, placement is unsafe and we prune this branch without recursing.
