Challenge - 5 Problems
Backtracking Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Backtracking Subset Sum
What is the output of the following C++ code that uses backtracking to find subsets summing to 5?
DSA C++
#include <iostream> #include <vector> using namespace std; void backtrack(vector<int>& nums, int target, int start, vector<int>& current, vector<vector<int>>& result) { if (target == 0) { for (int num : current) cout << num << " "; cout << "| "; return; } for (int i = start; i < nums.size(); i++) { if (nums[i] <= target) { current.push_back(nums[i]); backtrack(nums, target - nums[i], i + 1, current, result); current.pop_back(); } } } int main() { vector<int> nums = {1, 2, 3, 4, 5}; vector<int> current; vector<vector<int>> result; backtrack(nums, 5, 0, current, result); return 0; }
Attempts:
2 left
💡 Hint
Trace the recursion and note the order of elements and their sums.
✗ Incorrect
The code finds all subsets of nums that sum to 5 without reusing elements. The subsets are printed in the order found: {1,4}, {2,3}, and {5}.
🧠 Conceptual
intermediateBacktracking Decision Tree Nodes Count
If a backtracking algorithm explores all subsets of a set of size 4, how many nodes are there in the decision tree including the root?
Attempts:
2 left
💡 Hint
Each element can be either included or excluded, forming a binary tree.
✗ Incorrect
For n elements, the decision tree has 2^n leaves and 2^n - 1 internal nodes, total nodes = 2^(n+1) - 1. For n=4, total nodes = 31.
🔧 Debug
advancedIdentify the Backtracking Logic Error
What error does the following backtracking code produce when trying to generate permutations of {1,2,3}?
#include
#include
using namespace std;
void backtrack(vector& nums, vector& used, vector& current) {
if (current.size() == nums.size()) {
for (int x : current) cout << x << " ";
cout << "\n";
return;
}
for (int i = 0; i < nums.size(); i++) {
if (!used[i]) {
current.push_back(nums[i]);
backtrack(nums, used, current);
current.pop_back();
used[i] = false;
}
}
}
int main() {
vector nums = {1, 2, 3};
vector used(3, false);
vector current;
backtrack(nums, used, current);
return 0;
}
Attempts:
2 left
💡 Hint
Check when 'used[i]' is set to true and false.
✗ Incorrect
The code never sets used[i] to true before recursion, so the same element can be reused infinitely causing infinite recursion and no output.
❓ Predict Output
advancedOutput of N-Queens Backtracking
What is the output of the following code snippet that prints the number of solutions for 4-Queens problem using backtracking?
DSA C++
#include <iostream> #include <vector> using namespace std; bool isSafe(int row, int col, vector<int>& queens) { for (int i = 0; i < row; i++) { if (queens[i] == col || abs(queens[i] - col) == row - i) return false; } return true; } void solve(int row, int n, vector<int>& queens, int& count) { if (row == n) { count++; return; } for (int col = 0; col < n; col++) { if (isSafe(row, col, queens)) { queens[row] = col; solve(row + 1, n, queens, count); } } } int main() { int n = 4; vector<int> queens(n, -1); int count = 0; solve(0, n, queens, count); cout << count << endl; return 0; }
Attempts:
2 left
💡 Hint
Recall the known number of solutions for 4-Queens problem.
✗ Incorrect
The 4-Queens problem has exactly 2 distinct solutions. The code counts all valid placements.
🧠 Conceptual
expertBacktracking Pruning Effect on Decision Tree Size
How does pruning in backtracking affect the size of the decision tree compared to exploring all possibilities?
Attempts:
2 left
💡 Hint
Think about how skipping impossible paths affects the search space.
✗ Incorrect
Pruning stops exploring branches that cannot lead to a solution, thus reducing the number of nodes visited and effectively shrinking the decision tree size.
