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 the following Java code that generates all subsets (powerset) of the array [1, 2, 2]?
DSA Java
import java.util.*; public class Main { public static List<List<Integer>> subsetsWithDup(int[] nums) { List<List<Integer>> result = new ArrayList<>(); Arrays.sort(nums); backtrack(nums, 0, new ArrayList<>(), result); return result; } private static void backtrack(int[] nums, int start, List<Integer> tempList, List<List<Integer>> result) { result.add(new ArrayList<>(tempList)); for (int i = start; i < nums.length; i++) { if (i > start && nums[i] == nums[i - 1]) continue; tempList.add(nums[i]); backtrack(nums, i + 1, tempList, result); tempList.remove(tempList.size() - 1); } } public static void main(String[] args) { int[] nums = {1, 2, 2}; List<List<Integer>> subsets = subsetsWithDup(nums); System.out.println(subsets); } }
Attempts:
2 left
💡 Hint
Duplicates are skipped to avoid repeated subsets. The subsets are generated in sorted order.
✗ Incorrect
The code sorts the array and skips duplicates during recursion to avoid repeated subsets. The output lists all unique subsets including empty set.
❓ Predict Output
intermediateOutput of powerset generation for empty input
What is the output of this Java code that generates all subsets of an empty array?
DSA Java
import java.util.*; public class Main { public static List<List<Integer>> subsets(int[] nums) { List<List<Integer>> result = new ArrayList<>(); backtrack(nums, 0, new ArrayList<>(), result); return result; } private static void backtrack(int[] nums, int start, List<Integer> tempList, List<List<Integer>> result) { result.add(new ArrayList<>(tempList)); for (int i = start; i < nums.length; i++) { tempList.add(nums[i]); backtrack(nums, i + 1, tempList, result); tempList.remove(tempList.size() - 1); } } public static void main(String[] args) { int[] nums = {}; List<List<Integer>> subsets = subsets(nums); System.out.println(subsets); } }
Attempts:
2 left
💡 Hint
The powerset of an empty set contains only the empty set.
✗ Incorrect
Even for an empty input, the powerset contains one subset: the empty subset.
🔧 Debug
advancedIdentify the error in powerset generation code
What error does the following Java code produce when generating all subsets of [1, 2]?
DSA Java
import java.util.*; public class Main { public static List<List<Integer>> subsets(int[] nums) { List<List<Integer>> result = new ArrayList<>(); backtrack(nums, 0, new ArrayList<>(), result); return result; } private static void backtrack(int[] nums, int start, List<Integer> tempList, List<List<Integer>> result) { result.add(tempList); for (int i = start; i < nums.length; i++) { tempList.add(nums[i]); backtrack(nums, i + 1, tempList, result); tempList.remove(tempList.size() - 1); } } public static void main(String[] args) { int[] nums = {1, 2}; List<List<Integer>> subsets = subsets(nums); System.out.println(subsets); } }
Attempts:
2 left
💡 Hint
Check how subsets are added to the result list.
✗ Incorrect
The code adds the same list reference (tempList) multiple times without copying it, so all entries in result point to the same list which ends up as [1, 2].
❓ Predict Output
advancedOutput of iterative powerset generation
What is the output of this Java code that generates all subsets of [1, 2, 3] using an iterative approach?
DSA Java
import java.util.*; public class Main { public static List<List<Integer>> subsets(int[] nums) { List<List<Integer>> result = new ArrayList<>(); result.add(new ArrayList<>()); for (int num : nums) { int size = result.size(); for (int i = 0; i < size; i++) { List<Integer> newSubset = new ArrayList<>(result.get(i)); newSubset.add(num); result.add(newSubset); } } return result; } public static void main(String[] args) { int[] nums = {1, 2, 3}; List<List<Integer>> subsets = subsets(nums); System.out.println(subsets); } }
Attempts:
2 left
💡 Hint
The subsets are generated by adding each number to all existing subsets.
✗ Incorrect
The iterative method starts with empty subset and adds each number to all existing subsets, preserving order of generation.
🧠 Conceptual
expertNumber of subsets generated by powerset algorithm
Given an input array of length n, how many subsets does the powerset generation algorithm produce?
Attempts:
2 left
💡 Hint
Each element can either be included or excluded in a subset.
✗ Incorrect
For each element, there are two choices: include or exclude. So total subsets are 2 multiplied by itself n times = 2^n.
