Challenge - 5 Problems
Powerset Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of powerset generation with duplicates
What is the output of this Go code that generates all subsets (powerset) of the slice [1, 2]?
DSA Go
package main import "fmt" func subsets(nums []int) [][]int { res := [][]int{{}} for _, num := range nums { n := len(res) for i := 0; i < n; i++ { subset := append([]int{}, res[i]...) subset = append(subset, num) res = append(res, subset) } } return res } func main() { result := subsets([]int{1, 2}) fmt.Println(result) }
Attempts:
2 left
💡 Hint
Think about how subsets are built by adding each number to existing subsets.
✗ Incorrect
The code starts with an empty subset and for each number, it adds that number to all existing subsets, doubling the number of subsets each time. The order of subsets is preserved as shown in option C.
❓ Predict Output
intermediateOutput of recursive powerset function
What is the output of this recursive Go function generating all subsets of [3]?
DSA Go
package main import "fmt" func subsets(nums []int) [][]int { if len(nums) == 0 { return [][]int{{}} } first := nums[0] rest := subsets(nums[1:]) newSubsets := [][]int{} for _, subset := range rest { newSubset := append([]int{first}, subset...) newSubsets = append(newSubsets, newSubset) } return append(rest, newSubsets...) } func main() { result := subsets([]int{3}) fmt.Println(result) }
Attempts:
2 left
💡 Hint
Base case returns empty subset; recursive step adds first element to all subsets.
✗ Incorrect
The function returns the empty subset first, then adds the first element to each subset, resulting in [[], [3]].
🔧 Debug
advancedIdentify the error in powerset generation code
What error does this Go code produce when generating subsets of [1, 2]?
DSA Go
package main import "fmt" func subsets(nums []int) [][]int { res := [][]int{{}} for _, num := range nums { for _, subset := range res { subset = append(subset, num) res = append(res, subset) } } return res } func main() { result := subsets([]int{1, 2}) fmt.Println(result) }
Attempts:
2 left
💡 Hint
Appending to subsets inside the loop modifies the slice being iterated over.
✗ Incorrect
Appending to `res` while iterating over it with `range` causes an infinite loop because `len(res)` is re-evaluated each iteration and keeps increasing.
❓ Predict Output
advancedOutput of powerset with empty input slice
What is the output of this Go code generating all subsets of an empty slice?
DSA Go
package main import "fmt" func subsets(nums []int) [][]int { res := [][]int{{}} for _, num := range nums { n := len(res) for i := 0; i < n; i++ { subset := append([]int{}, res[i]...) subset = append(subset, num) res = append(res, subset) } } return res } func main() { result := subsets([]int{}) fmt.Println(result) }
Attempts:
2 left
💡 Hint
The powerset of empty set contains one subset: the empty set itself.
✗ Incorrect
The function initializes res with [[]], so the output is a slice containing one empty slice.
🧠 Conceptual
expertNumber of subsets generated by powerset function
If the input slice has length n, how many subsets does the powerset function generate?
Attempts:
1 left
💡 Hint
Each element can either be in or out of a subset.
✗ Incorrect
Each element has two choices: included or excluded, so total subsets are 2^n.
