Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to generate all subsets of nums using bitmask.
DSA Go
for mask := 0; mask < (1 << [1]); mask++ { subset := []int{} for i := 0; i < len(nums); i++ { if mask&(1<<i) != 0 { subset = append(subset, nums[i]) } } result = append(result, subset) }
Attempts:
3 left
💡 Hint
Common Mistakes
Using cap(nums) instead of len(nums)
Using a fixed number instead of length
Using nums[0] which is a value, not length
✗ Incorrect
We use len(nums) to know how many bits we need to represent all subsets.
2fill in blank
mediumComplete the code to check if the i-th bit is set in mask.
DSA Go
if mask & ([1] << i) != 0 { subset = append(subset, nums[i]) }
Attempts:
3 left
💡 Hint
Common Mistakes
Using mask instead of 1 for shifting
Using i without shifting
Using 0 which always results in zero
✗ Incorrect
We shift 1 by i to check if the i-th bit in mask is set.
3fill in blank
hardFix the error in the code to append subsets correctly.
DSA Go
result = append(result, [1])Attempts:
3 left
💡 Hint
Common Mistakes
Appending nums[i] instead of subset
Appending mask which is an integer
Appending the entire nums slice every time
✗ Incorrect
We append the current subset slice, not a single element or mask.
4fill in blank
hardFill both blanks to create a map of subset sums and their counts.
DSA Go
sumCount := map[int]int{}
for mask := 0; mask < (1 << [1]); mask++ {
sum := 0
for i := 0; i < [2]; i++ {
if mask&(1<<i) != 0 {
sum += nums[i]
}
}
sumCount[sum]++
}Attempts:
3 left
💡 Hint
Common Mistakes
Using cap(nums) which may differ from length
Using len(nums) - 1 which misses last element
Using len(result) which is unrelated
✗ Incorrect
Both blanks use len(nums) to iterate over all elements correctly.
5fill in blank
hardFill all three blanks to create a map of subsets keyed by their bitmask.
DSA Go
subsetMap := map[int][]int{}
for [1] := 0; [2] < (1 << len(nums)); [2]++ {
subset := []int{}
for i := 0; i < len(nums); i++ {
if [2]&(1<<i) != 0 {
subset = append(subset, nums[i])
}
}
subsetMap[[2]] = subset
}Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names for loop and map key
Using i as loop variable in outer loop
Using count which is undefined
✗ Incorrect
The loop variable is mask, used consistently to track subsets and keys.
