Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to calculate the total number of subsets for a set of size n.
DSA C++
int total_subsets = 1 << [1];
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable name instead of n.
Using addition instead of bit shift.
✗ Incorrect
Using bit shifting 1 << n calculates 2 to the power n, which is the total number of subsets.
2fill in blank
mediumComplete the loop to iterate over all subsets represented by bitmasks.
DSA C++
for (int mask = 0; mask < (1 << [1]); mask++) { // process subset }
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable for the set size.
Using <= instead of < in the loop condition.
✗ Incorrect
The loop runs from 0 to 2^n - 1, covering all subsets represented by bitmasks.
3fill in blank
hardFix the error in the condition to check if the j-th element is in the current subset mask.
DSA C++
if (mask & (1 << [1])) { subset.push_back(nums[j]); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using i instead of j for the bit position.
Using mask or n which are not bit positions.
✗ Incorrect
We check the j-th bit in mask to see if the j-th element is included in the subset.
4fill in blank
hardFill both blanks to complete the inner loop that builds each subset from the bitmask.
DSA C++
for (int j = 0; j < [1]; j++) { if (mask & (1 << [2])) { subset.push_back(nums[j]); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using i instead of j in the bit check.
Using wrong loop limit.
✗ Incorrect
The inner loop runs from 0 to n-1, and checks the j-th bit in mask to decide if nums[j] is included.
5fill in blank
hardFill all three blanks to complete the function that generates all subsets using bitmask.
DSA C++
vector<vector<int>> subsets(vector<int>& nums) {
int [1] = nums.size();
vector<vector<int>> result;
for (int mask = 0; mask < (1 << [2]); mask++) {
vector<int> subset;
for (int j = 0; j < [3]; j++) {
if (mask & (1 << j)) {
subset.push_back(nums[j]);
}
}
result.push_back(subset);
}
return result;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Using wrong loop limits.
✗ Incorrect
We store the size in n, use n for the loop limit and bitmask range to generate all subsets.
