Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start the backtracking with an empty combination.
DSA Go
func combinationSumK(candidates []int, k int) [][]int {
var result [][]int
var comb []int
backtrack(candidates, k, 0, comb, &result)
return result
}
func backtrack(candidates []int, target int, start int, comb []int, result *[][]int) {
if target == 0 {
*result = append(*result, append([]int{}, comb...))
return
}
for i := start; i < len(candidates); i++ {
if candidates[i] > target {
continue
}
comb = append(comb, candidates[i])
backtrack(candidates, target[1], i+1, comb, result)
comb = comb[:len(comb)-1]
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Adding the candidate to the target instead of subtracting.
Multiplying or dividing the target by the candidate.
✗ Incorrect
We subtract the current candidate from the target to reduce the remaining sum needed.
2fill in blank
mediumComplete the code to avoid reusing the same candidate index in combinations.
DSA Go
func backtrack(candidates []int, target int, start int, comb []int, result *[][]int) {
if target == 0 {
*result = append(*result, append([]int{}, comb...))
return
}
for i := start; i < len(candidates); i++ {
if candidates[i] > target {
continue
}
comb = append(comb, candidates[i])
backtrack(candidates, target-candidates[i], [1], comb, result)
comb = comb[:len(comb)-1]
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using the same index repeatedly causing infinite loops.
Resetting start to 0 causing duplicates.
✗ Incorrect
We move to the next index (i+1) to avoid reusing the same candidate in the current combination.
3fill in blank
hardFix the error in the base case to correctly add a copy of the current combination to the result.
DSA Go
if target == 0 { *result = append(*result, [1]) return }
Attempts:
3 left
💡 Hint
Common Mistakes
Appending comb directly causing all results to be the same.
Using copy() which returns number of elements copied, not a slice.
✗ Incorrect
We must append a copy of comb to avoid later modifications affecting stored combinations.
4fill in blank
hardFill both blanks to skip candidates that are greater than the remaining target.
DSA Go
for i := start; i < len(candidates); i++ { if candidates[i] [1] target { [2] } comb = append(comb, candidates[i]) backtrack(candidates, target-candidates[i], i+1, comb, result) comb = comb[:len(comb)-1] }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' causing wrong skips.
Using 'break' which stops the loop prematurely.
✗ Incorrect
If candidate is greater than target, we skip it with continue to try next candidate.
5fill in blank
hardFill all three blanks to define the function signature and initialize variables correctly.
DSA Go
func [1](candidates []int, k int) [][]int { var [2] [][]int var comb []int backtrack(candidates, k, 0, comb, &[3]) return [2] }
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatching variable names causing compilation errors.
Not passing result by reference.
✗ Incorrect
Function is named combinationSumK; result slice is named result and passed by reference.
