Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start generating subsets by calling the helper function.
DSA Go
func subsets(nums []int) [][]int {
result := [][]int{}
helper(nums, 0, []int{}, &result)
return [1]
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning the input slice instead of the result.
Returning the helper function call instead of the result.
✗ Incorrect
The function returns the collected subsets stored in 'result'.
2fill in blank
mediumComplete the code to add the current subset copy to the result.
DSA Go
func helper(nums []int, index int, current []int, result *[][]int) {
if index == len(nums) {
temp := make([]int, len(current))
copy(temp, current)
*result = append(*result, [1])
return
}
// recursive calls omitted for brevity
}Attempts:
3 left
💡 Hint
Common Mistakes
Appending 'current' directly causing all subsets to be the same.
Appending the original 'nums' slice.
✗ Incorrect
We append a copy 'temp' of the current subset to avoid mutation issues.
3fill in blank
hardFix the error in the recursive call to include the next element in the subset.
DSA Go
func helper(nums []int, index int, current []int, result *[][]int) {
if index == len(nums) {
// base case
return
}
// exclude current element
helper(nums, index+1, current, result)
// include current element
helper(nums, index+1, [1], result)
}Attempts:
3 left
💡 Hint
Common Mistakes
Passing 'current' without adding the element.
Appending to 'nums' or 'result' incorrectly.
✗ Incorrect
We create a new slice by appending the current element to 'current' for the include case.
4fill in blank
hardFill both blanks to correctly start the helper function and append the current subset copy.
DSA Go
func subsets(nums []int) [][]int {
result := [][]int{}
[1](nums, 0, []int{}, &result)
return result
}
func helper(nums []int, index int, current []int, result *[][]int) {
if index == len(nums) {
temp := make([]int, len(current))
copy(temp, current)
*result = append(*result, [2])
return
}
helper(nums, index+1, current, result)
helper(nums, index+1, append(current, nums[index]), result)
}Attempts:
3 left
💡 Hint
Common Mistakes
Calling the wrong function name.
Appending the original 'current' slice instead of the copy.
✗ Incorrect
Call 'helper' to start recursion and append the copied slice 'temp' to result.
5fill in blank
hardFill all three blanks to complete the helper function with base case, exclude, and include logic.
DSA Go
func helper(nums []int, index int, current []int, result *[][]int) {
if [1] == len(nums) {
temp := make([]int, len(current))
copy(temp, current)
*result = append(*result, temp)
return
}
helper(nums, index+1, current, result) // exclude
helper(nums, index+1, [2], result) // include
}
func subsets(nums []int) [][]int {
result := [][]int{}
[3](nums, 0, []int{}, &result)
return result
}Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable in base case.
Not appending element correctly.
Calling wrong function name.
✗ Incorrect
Check if 'index' reached end, include element by appending, and call 'helper' to start recursion.
