Challenge - 5 Problems
Backtracking Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Backtracking for Subset Sum Problem
Consider the following Go code that uses backtracking to find if there is a subset of numbers that sums to a target. What is the printed output after running this code?
DSA Go
package main import "fmt" func subsetSum(nums []int, target int, index int) bool { if target == 0 { return true } if target < 0 || index == len(nums) { return false } // Include nums[index] if subsetSum(nums, target-nums[index], index+1) { return true } // Exclude nums[index] return subsetSum(nums, target, index+1) } func main() { nums := []int{3, 34, 4, 12, 5, 2} target := 9 if subsetSum(nums, target, 0) { fmt.Println("Subset found") } else { fmt.Println("No subset found") } }
Attempts:
2 left
💡 Hint
Think about whether any subset of nums sums exactly to 9.
✗ Incorrect
The subset {4, 5} sums to 9, so the backtracking function returns true and prints 'Subset found'.
❓ Predict Output
intermediateGreedy Algorithm Failure on Coin Change
What is the output of this Go code that tries to find minimum coins for amount 6 using greedy approach with coins [1, 3, 4]?
DSA Go
package main import "fmt" func minCoinsGreedy(coins []int, amount int) int { count := 0 for i := len(coins) - 1; i >= 0; i-- { for amount >= coins[i] { amount -= coins[i] count++ } } if amount != 0 { return -1 } return count } func main() { coins := []int{1, 3, 4} amount := 6 result := minCoinsGreedy(coins, amount) fmt.Println(result) }
Attempts:
2 left
💡 Hint
Greedy picks largest coin first, but is it optimal here?
✗ Incorrect
Greedy picks 4 then two 1s, total 3 coins. But optimal is two 3s (2 coins). Greedy returns 3.
🧠 Conceptual
advancedWhy Backtracking is Needed Over Greedy
Which problem below cannot be solved correctly by a greedy algorithm but requires backtracking to explore all possibilities?
Attempts:
2 left
💡 Hint
Think about problems where you must try many combinations.
✗ Incorrect
Finding all subsets that sum to a target requires exploring many combinations, which greedy cannot do correctly.
❓ Predict Output
advancedBacktracking Path Exploration Output
What is the output of this Go code that prints all subsets of [1,2] using backtracking?
DSA Go
package main import "fmt" func backtrack(nums []int, index int, path []int) { if index == len(nums) { fmt.Print("Subset: ") for _, v := range path { fmt.Print(v, " ") } fmt.Println() return } // Include nums[index] backtrack(nums, index+1, append(path, nums[index])) // Exclude nums[index] backtrack(nums, index+1, path) } func main() { nums := []int{1, 2} backtrack(nums, 0, []int{}) }
Attempts:
2 left
💡 Hint
Backtracking includes subsets with and without each element.
✗ Incorrect
The code prints all subsets: {1,2}, {1}, {2}, and empty subset in that order.
🧠 Conceptual
expertWhy Greedy Fails for Certain Optimization Problems
Which statement best explains why greedy algorithms fail for some problems that backtracking can solve?
Attempts:
2 left
💡 Hint
Consider how greedy chooses steps versus backtracking's exploration.
✗ Incorrect
Greedy picks the best immediate choice, which can miss better overall solutions. Backtracking tries all options to find the best.
