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 Recursion
What is the output of the following C++ code that generates all subsets (powerset) of the set {1, 2}?
DSA C++
#include <iostream> #include <vector> using namespace std; void generateSubsets(vector<int>& nums, int index, vector<int>& current, vector<vector<int>>& result) { if (index == nums.size()) { result.push_back(current); return; } // Include nums[index] current.push_back(nums[index]); generateSubsets(nums, index + 1, current, result); // Exclude nums[index] current.pop_back(); generateSubsets(nums, index + 1, current, result); } int main() { vector<int> nums = {1, 2}; vector<vector<int>> result; vector<int> current; generateSubsets(nums, 0, current, result); for (auto& subset : result) { cout << "["; for (int i = 0; i < subset.size(); i++) { cout << subset[i]; if (i != subset.size() - 1) cout << ", "; } cout << "]\n"; } return 0; }
Attempts:
2 left
💡 Hint
Think about the order subsets are added when including or excluding elements recursively.
✗ Incorrect
The recursion explores the include branch first, adding [1, 2] , then backtracks to [1], then excludes 1 to add [2] and [], resulting in the order: [1, 2], [1], [2], [].
🧠 Conceptual
intermediateNumber of Subsets in Powerset
Given a set with 5 distinct elements, how many subsets does its powerset contain?
Attempts:
2 left
💡 Hint
Each element can either be in or out of a subset.
✗ Incorrect
For a set with n elements, the powerset contains 2^n subsets. For n=5, 2^5 = 32.
🔧 Debug
advancedIdentify the Error in Powerset Code
What error will the following C++ code produce when generating all subsets of {1, 2}?
DSA C++
#include <iostream> #include <vector> using namespace std; void generateSubsets(vector<int>& nums, int index, vector<int>& current, vector<vector<int>>& result) { if (index > nums.size()) { result.push_back(current); return; } current.push_back(nums[index]); generateSubsets(nums, index + 1, current, result); current.pop_back(); generateSubsets(nums, index + 1, current, result); } int main() { vector<int> nums = {1, 2}; vector<vector<int>> result; vector<int> current; generateSubsets(nums, 0, current, result); for (auto& subset : result) { cout << "["; for (int i = 0; i < subset.size(); i++) { cout << subset[i]; if (i != subset.size() - 1) cout << ", "; } cout << "]\n"; } return 0; }
Attempts:
2 left
💡 Hint
Check the base case condition for index.
✗ Incorrect
The base case checks if index > nums.size(), but valid indices are 0 to nums.size()-1. Accessing nums[index] when index == nums.size() causes out_of_range exception.
🚀 Application
advancedFind Subsets with Sum Equal to Target
Given the set {1, 2, 3} and target sum 3, which subsets from the powerset sum exactly to 3?
Attempts:
2 left
💡 Hint
Check sums of all subsets carefully.
✗ Incorrect
Subsets summing to 3 are [3] and [1, 2]. Others do not sum to 3.
📝 Syntax
expertCorrect Syntax for Iterative Powerset Generation
Which option correctly implements iterative generation of all subsets of {1, 2} in C++?
Attempts:
2 left
💡 Hint
Check the initialization of subsets vector and syntax of braces.
✗ Incorrect
Option C correctly initializes subsets with one empty subset using {{}} and has correct braces. Others have syntax errors or wrong initialization.
