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
Think about all subsets of nums that add up exactly to 5 without reuse.
✗ Incorrect
The code finds all subsets of the array {1,2,3,4,5} that sum to 5 using backtracking. The valid subsets are {1,4}, {2,3}, and {5}.
🧠 Conceptual
intermediateWhy Greedy Fails for Subset Sum
Why does a greedy algorithm fail to solve the subset sum problem correctly in all cases?
Attempts:
2 left
💡 Hint
Consider if picking the largest number first always leads to a correct subset sum.
✗ Incorrect
Greedy algorithms make the best local choice at each step but may miss the correct combination that requires skipping a local best choice.
🔧 Debug
advancedIdentify the Error in Greedy Coin Change
What error does the following greedy coin change code produce when given coins {1,3,4} and amount 6?
DSA C++
#include <iostream> #include <vector> using namespace std; int greedyCoinChange(vector<int>& coins, int amount) { int count = 0; for (int i = coins.size() - 1; i >= 0; i--) { while (amount >= coins[i]) { amount -= coins[i]; count++; } } return amount == 0 ? count : -1; } int main() { vector<int> coins = {1, 3, 4}; cout << greedyCoinChange(coins, 6); return 0; }
Attempts:
2 left
💡 Hint
Check how greedy picks coins starting from largest coin.
✗ Incorrect
The greedy picks coin 4 first, then two coins of 1, total 3 coins, but minimum coins needed is 2 (3+3). So output is 3 which is incorrect.
❓ Predict Output
advancedBacktracking N-Queens Output Count
What is the output count of solutions printed by this backtracking code for N=4?
DSA C++
#include <iostream> #include <vector> using namespace std; bool isSafe(vector<int>& board, int row, int col) { for (int i = 0; i < row; i++) { if (board[i] == col || abs(board[i] - col) == row - i) return false; } return true; } void solveNQueens(int n, int row, vector<int>& board, int& count) { if (row == n) { count++; return; } for (int col = 0; col < n; col++) { if (isSafe(board, row, col)) { board[row] = col; solveNQueens(n, row + 1, board, count); } } } int main() { int n = 4; vector<int> board(n); int count = 0; solveNQueens(n, 0, board, count); cout << count; return 0; }
Attempts:
2 left
💡 Hint
N-Queens for 4 has exactly 2 solutions.
✗ Incorrect
The classic 4-Queens problem has 2 distinct solutions found by backtracking.
🧠 Conceptual
expertWhy Backtracking is Needed Over Greedy for Hamiltonian Path
Why is backtracking necessary to find a Hamiltonian path in a graph instead of a greedy approach?
Attempts:
2 left
💡 Hint
Think about the complexity of visiting all nodes exactly once.
✗ Incorrect
Greedy algorithms make local choices and cannot ensure a path visiting all vertices exactly once, which requires exploring all possibilities via backtracking.
