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 Check
What is the output of this Rust code that uses backtracking to check if a subset sums to a target?
DSA Rust
fn subset_sum(nums: &[i32], target: i32, index: usize) -> bool { if target == 0 { return true; } if index == nums.len() || target < 0 { return false; } subset_sum(nums, target - nums[index], index + 1) || subset_sum(nums, target, index + 1) } fn main() { let nums = [3, 34, 4, 12, 5, 2]; let target = 9; if subset_sum(&nums, target, 0) { println!("Found a subset with sum {}", target); } else { println!("No subset with sum {}", target); } }
Attempts:
2 left
💡 Hint
Think about whether any combination of numbers can add up to 9.
✗ Incorrect
The function tries all subsets recursively. The subset {4, 5} sums to 9, so it returns true and prints the success message.
🧠 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
Think about how greedy chooses elements and what it might miss.
✗ Incorrect
Greedy picks the largest number first, which can cause it to miss smaller numbers that together form the target sum. Subset sum requires exploring all combinations, which greedy does not do.
❓ Predict Output
advancedOutput of Greedy Coin Change on Non-Canonical Set
What is the output of this Rust code using a greedy algorithm to make change with coins [1, 3, 4] for amount 6?
DSA Rust
fn greedy_coin_change(coins: &[i32], amount: i32) -> i32 {
let mut remaining = amount;
let mut count = 0;
for &coin in coins.iter().rev() {
count += remaining / coin;
remaining %= coin;
}
count
}
fn main() {
let coins = [1, 3, 4];
let amount = 6;
let result = greedy_coin_change(&coins, amount);
println!("Coins used: {}", result);
}Attempts:
2 left
💡 Hint
Check how greedy picks coins starting from largest to smallest.
✗ Incorrect
Greedy picks one 4-coin, then two 1-coins, total 3 coins. But optimal is two 3-coins (2 coins). Greedy fails here.
🧠 Conceptual
advancedBacktracking Advantage Over Greedy
Which statement best explains why backtracking can solve problems greedy cannot?
Attempts:
2 left
💡 Hint
Think about how backtracking tries options compared to greedy.
✗ Incorrect
Backtracking tries every possible choice and backtracks if it fails, ensuring all solutions are considered. Greedy picks the best local option and never revisits choices.
❓ Predict Output
expertOutput of Backtracking N-Queens Solution Count
What is the output of this Rust code that counts solutions to the 4-queens problem using backtracking?
DSA Rust
fn is_safe(board: &[usize], row: usize, col: usize) -> bool { for i in 0..row { if board[i] == col || (board[i] as isize - col as isize).abs() == (row as isize - i as isize).abs() { return false; } } true } fn solve_n_queens(n: usize, row: usize, board: &mut Vec<usize>, count: &mut usize) { if row == n { *count += 1; return; } for col in 0..n { if is_safe(board, row, col) { board[row] = col; solve_n_queens(n, row + 1, board, count); } } } fn main() { let n = 4; let mut board = vec![0; n]; let mut count = 0; solve_n_queens(n, 0, &mut board, &mut count); println!("Number of solutions: {}", count); }
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 using backtracking.
