Challenge - 5 Problems
Backtracking Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Backtracking Path Exploration
What is the printed output of the following Go code that uses backtracking to find all subsets of {1, 2}?
DSA Go
package main import "fmt" func backtrack(nums []int, start int, path []int) { fmt.Println(path) for i := start; i < len(nums); i++ { backtrack(nums, i+1, append(path, nums[i])) } } func main() { backtrack([]int{1, 2}, 0, []int{}) }
Attempts:
2 left
💡 Hint
Trace the recursive calls and note when the path is printed.
✗ Incorrect
The function prints the current path before exploring further elements. It starts with an empty path, then adds 1, then adds 2, then backtracks to add 2 alone.
🧠 Conceptual
intermediateBacktracking Decision Tree Nodes Count
How many nodes are there in the decision tree when backtracking to generate all permutations of 3 distinct elements?
Attempts:
2 left
💡 Hint
Count all partial and complete permutations including the root node.
✗ Incorrect
For 3 elements, the decision tree has 1 (root) + 3 + 6 + 6 = 16 nodes representing all partial and full permutations.
🔧 Debug
advancedIdentify the Backtracking Logic Error
What error does the following Go backtracking code produce when trying to generate combinations of size 2 from {1,2,3}?
DSA Go
package main import "fmt" func backtrack(nums []int, start int, path []int, k int) { if len(path) == k { fmt.Println(path) return } for i := start; i < len(nums); i++ { backtrack(nums, i+1, append(path, nums[i]), k) } } func main() { backtrack([]int{1,2,3}, 0, []int{}, 2) }
Attempts:
2 left
💡 Hint
Check the loop boundary condition and array indexing.
✗ Incorrect
The loop uses i <= len(nums), which causes an index out of range error when i equals len(nums).
🚀 Application
advancedBacktracking to Solve N-Queens: Number of Solutions
Using backtracking, how many distinct solutions exist for the 4-Queens problem?
Attempts:
2 left
💡 Hint
Recall the classic known count of 4-Queens solutions.
✗ Incorrect
The 4-Queens problem has exactly 2 distinct solutions found by backtracking.
❓ Predict Output
expertBacktracking with Pruning: Output of Partial Paths
What is the output of this Go code that uses backtracking with pruning to generate subsets of {1,2,3} where the sum is less than 4?
DSA Go
package main import "fmt" func backtrack(nums []int, start int, path []int, sum int) { if sum >= 4 { return } fmt.Println(path) for i := start; i < len(nums); i++ { backtrack(nums, i+1, append(path, nums[i]), sum+nums[i]) } } func main() { backtrack([]int{1,2,3}, 0, []int{}, 0) }
Attempts:
2 left
💡 Hint
Trace sums and note when pruning stops recursion.
✗ Incorrect
The pruning check occurs before printing, so paths with sum >=4 like [1 3] (sum=4), [2 3] (5), [1 2 3] (6) are pruned without printing.
