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 code that generates all subsets (powerset) of the list [1, 2, 2]?
DSA Python
def powerset(nums): result = [[]] for num in nums: result += [curr + [num] for curr in result] return result print(powerset([1, 2, 2]))
Attempts:
2 left
💡 Hint
Think about how duplicates in the input list affect the subsets generated.
✗ Incorrect
The code adds each number to all existing subsets, so duplicates cause repeated subsets. The output includes duplicates subsets like [2] twice.
❓ Predict Output
intermediateNumber of subsets generated
How many subsets does the powerset function generate for the input list [3, 5, 7]?
DSA Python
def powerset(nums): result = [[]] for num in nums: result += [curr + [num] for curr in result] return result print(len(powerset([3, 5, 7])))
Attempts:
2 left
💡 Hint
Number of subsets for a list of length n is 2^n.
✗ Incorrect
For 3 elements, total subsets = 2^3 = 8.
🔧 Debug
advancedIdentify the error in this powerset code
What error does this code raise when run?
DSA Python
def powerset(nums): result = [] for num in nums: result += [curr + [num] for curr in result] return result print(powerset([1, 2]))
Attempts:
2 left
💡 Hint
Look at the initial value of result and how list concatenation is used.
✗ Incorrect
result starts as empty list, so list comprehension uses curr from empty list, causing error when adding int and list.
🧠 Conceptual
advancedWhy does the powerset size grow exponentially?
Why does the number of subsets in a powerset grow exponentially with the size of the input list?
Attempts:
2 left
💡 Hint
Think about choices for each element in subset formation.
✗ Incorrect
Each element has two choices: include or exclude, so total subsets = 2^n.
🚀 Application
expertFind subsets with sum equal to target
Given the powerset function below, which option correctly returns all subsets of [2, 4, 6] whose sum is 6?
DSA Python
def powerset(nums): result = [[]] for num in nums: result += [curr + [num] for curr in result] return result nums = [2, 4, 6] all_subsets = powerset(nums) # Which option filters subsets with sum 6 correctly?
Attempts:
2 left
💡 Hint
You want subsets whose sum is exactly 6.
✗ Incorrect
Option A filters subsets whose sum equals 6 exactly, others do not.
