Challenge - 5 Problems
Combination Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Generate All Combinations Sum K with candidates [2,3,6,7]
What is the output of the following Go code that finds all unique combinations of candidates that sum to 7?
DSA Go
package main import ( "fmt" ) func combinationSum(candidates []int, target int) [][]int { var res [][]int var comb []int var backtrack func(start, target int) backtrack = func(start, target int) { if target == 0 { combCopy := make([]int, len(comb)) copy(combCopy, comb) res = append(res, combCopy) return } for i := start; i < len(candidates); i++ { if candidates[i] > target { continue } comb = append(comb, candidates[i]) backtrack(i, target - candidates[i]) comb = comb[:len(comb)-1] } } backtrack(0, target) return res } func main() { candidates := []int{2, 3, 6, 7} target := 7 result := combinationSum(candidates, target) fmt.Println(result) }
Attempts:
2 left
💡 Hint
Think about how the function explores combinations by including candidates multiple times and stops when the sum matches the target.
✗ Incorrect
The function uses backtracking to find all combinations of candidates that sum to the target 7. It includes 2 twice and 3 once to make 7, and also includes 7 alone. Other options either have wrong sums or invalid combinations.
🧠 Conceptual
intermediateNumber of combinations for candidates [1,2,3] and target 4
How many unique combinations of candidates [1, 2, 3] sum up to 4 if each candidate can be used unlimited times?
Attempts:
2 left
💡 Hint
Try to list all combinations manually or think about the recursive tree of choices.
✗ Incorrect
The unique combinations (in non-decreasing order) are: [1,1,1,1], [1,1,2], [1,3], [2,2]. There are 4 such combinations.
🔧 Debug
advancedIdentify the error in this combination sum code snippet
What error does the following Go code produce when trying to generate combinations summing to 5?
func combinationSum(candidates []int, target int) [][]int {
var res [][]int
var comb []int
var backtrack func(int, int)
backtrack = func(start int, target int) {
if target == 0 {
res = append(res, comb)
return
}
for i := start; i < len(candidates); i++ {
if candidates[i] > target {
continue
}
comb = append(comb, candidates[i])
backtrack(i, target - candidates[i])
comb = comb[:len(comb)-1]
}
}
backtrack(0, target)
return res
}
Attempts:
2 left
💡 Hint
Think about what happens when you append a slice reference to the result multiple times.
✗ Incorrect
The code appends the slice 'comb' directly to 'res' without copying it. Since 'comb' is modified later, all entries in 'res' point to the same underlying slice, causing all results to be identical.
🚀 Application
advancedFind all combinations summing to 8 with candidates [2,3,5]
What is the output of the Go function that finds all unique combinations of candidates [2,3,5] that sum to 8?
DSA Go
package main import "fmt" func combinationSum(candidates []int, target int) [][]int { var res [][]int var comb []int var backtrack func(int, int) backtrack = func(start int, target int) { if target == 0 { combCopy := make([]int, len(comb)) copy(combCopy, comb) res = append(res, combCopy) return } for i := start; i < len(candidates); i++ { if candidates[i] > target { continue } comb = append(comb, candidates[i]) backtrack(i, target - candidates[i]) comb = comb[:len(comb)-1] } } backtrack(0, target) return res } func main() { candidates := []int{2, 3, 5} target := 8 result := combinationSum(candidates, target) fmt.Println(result) }
Attempts:
2 left
💡 Hint
Remember combinations are sorted by candidate index and candidates can be reused.
✗ Incorrect
The valid combinations summing to 8 are four 2's, two 3's plus one 2, and one 3 plus one 5. The order inside each combination respects the candidate order.
🧠 Conceptual
expertTime complexity of Generate All Combinations Sum K
What is the time complexity of generating all unique combinations of candidates that sum to target K, where each candidate can be used unlimited times?
Attempts:
2 left
💡 Hint
Consider how many combinations can be formed when candidates can be repeated and sum to K.
✗ Incorrect
The number of combinations grows exponentially with target K because candidates can be reused unlimited times, leading to a large search tree roughly O(N^K).
