Challenge - 5 Problems
Powerset Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
easyPowerset Size
How many subsets does a set of n elements have, including the empty set?
Attempts:
2 left
💡 Hint
Each element has exactly two choices: included or excluded. Apply the multiplication rule.
✗ Incorrect
For each of the n elements, there are exactly 2 choices: include it or exclude it. By the multiplication principle, total subsets = 2 × 2 × ... × 2 (n times) = 2^n. This includes the empty set (all elements excluded) and the full set (all elements included).
❓ Predict Output
intermediateTrace Backtracking Subset Generation
What does this C# code output for nums = [1, 2]?
DSA C#
void Generate(int[] nums, int start, List<int> curr, List<List<int>> res) { res.Add(new List<int>(curr)); for (int i = start; i < nums.Length; i++) { curr.Add(nums[i]); Generate(nums, i + 1, curr, res); curr.RemoveAt(curr.Count - 1); } } var result = new List<List<int>>(); Generate(new int[]{1,2}, 0, new List<int>(), result); Console.WriteLine(result.Count);
Attempts:
2 left
💡 Hint
2^2 = 4 subsets for 2 elements: {}, {1}, {2}, {1,2}.
✗ Incorrect
Trace: start=0, curr=[] → add []. i=0: add 1→curr=[1], recurse start=1 → add [1]. i=1: add 2→curr=[1,2] → add [1,2]. Backtrack→curr=[1]. Backtrack→curr=[]. i=1: add 2→curr=[2] → add [2]. Backtrack→curr=[]. Total: 4 subsets → output 4.
🧠 Conceptual
intermediateWhy Copy the List Before Adding
In C# subset generation, why must we write res.Add(new List(curr)) instead of res.Add(curr)?
Attempts:
2 left
💡 Hint
In C#, List is a reference type. What happens when the same object is modified after being added to another collection?
✗ Incorrect
List is a reference type in C#. If you write res.Add(curr), you add a reference to the same curr list. As backtracking continues, curr is modified — elements added and removed. All entries in res that reference curr will reflect these changes, so at the end all entries show curr's final state (empty). new List(curr) creates an independent copy capturing the current state.
🔧 Debug
intermediateIdentify the Duplicate Subset Bug
This code generates duplicate subsets. Which line causes the bug?
DSA C#
void Generate(int[] nums, int start, List<int> curr, List<List<int>> res) { res.Add(new List<int>(curr)); for (int i = 0; i < nums.Length; i++) { // LINE A curr.Add(nums[i]); Generate(nums, i + 1, curr, res); // LINE B curr.RemoveAt(curr.Count - 1); } }
Attempts:
2 left
💡 Hint
What happens when i starts from 0 instead of start? Can element at index 0 appear after element at index 2?
✗ Incorrect
Starting from i=0 instead of start means at each recursive level we can re-pick elements that were already chosen at a higher level. This generates subsets like [2,1] (which is a duplicate of [1,2] for sets). The loop must start from 'start' to ensure each element is only considered in its original order — preventing both duplicates and out-of-order subsets.
🚀 Application
hardSubsets with Duplicates
For nums = [1, 2, 2], how many distinct subsets does the standard powerset algorithm produce (after sorting and deduplication at each recursive level)?
Attempts:
2 left
💡 Hint
List the distinct subsets: {}, {1}, {2}, {1,2}, {2,2}, {1,2,2}.
✗ Incorrect
The distinct subsets of [1,2,2] are: {} (empty), {1}, {2}, {2,2}, {1,2}, {1,2,2} — 6 total. The standard approach sorts the array and skips nums[i] if i > start and nums[i] == nums[i-1] at each level to avoid generating the same subset twice from duplicate elements.
