Challenge - 5 Problems
Bitmask Subsets Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of subsets generated by bitmask
What is the output of the following JavaScript code that generates all subsets of [1, 2] using bitmask?
DSA Javascript
const nums = [1, 2]; const subsets = []; for (let mask = 0; mask < (1 << nums.length); mask++) { const subset = []; for (let i = 0; i < nums.length; i++) { if (mask & (1 << i)) { subset.push(nums[i]); } } subsets.push(subset); } console.log(subsets);
Attempts:
2 left
💡 Hint
Think about how bitmask represents subsets by including elements where bits are set.
✗ Incorrect
The code uses bitmask from 0 to 3 (2^2 - 1). For each mask, it includes elements where the bit is 1. Mask 0 means no elements, mask 1 means first element, mask 2 means second element, mask 3 means both elements.
❓ Predict Output
intermediateNumber of subsets generated by bitmask
How many subsets will be generated by the following code for the array [5, 10, 15]?
DSA Javascript
const nums = [5, 10, 15]; const subsets = []; for (let mask = 0; mask < (1 << nums.length); mask++) { const subset = []; for (let i = 0; i < nums.length; i++) { if (mask & (1 << i)) { subset.push(nums[i]); } } subsets.push(subset); } console.log(subsets.length);
Attempts:
2 left
💡 Hint
Number of subsets of an array of length n is 2^n.
✗ Incorrect
For an array of length 3, total subsets are 2^3 = 8, including the empty subset.
🔧 Debug
advancedIdentify the error in bitmask subset generation
What error will the following code produce when generating subsets using bitmask for [1, 2, 3]?
DSA Javascript
const nums = [1, 2, 3]; const subsets = []; for (let mask = 0; mask <= (1 << nums.length); mask++) { const subset = []; for (let i = 0; i < nums.length; i++) { if (mask & (1 << i)) { subset.push(nums[i]); } } subsets.push(subset); } console.log(subsets.length);
Attempts:
2 left
💡 Hint
Check the loop condition for mask and how many iterations it runs.
✗ Incorrect
The loop runs from mask = 0 to mask = 8 inclusive, so total 9 iterations, but subsets for length 3 should be 8.
❓ Predict Output
advancedOutput of subsets with filtering condition
What is the output of the following code that generates subsets of [1, 2, 3] but only includes subsets with sum greater than 3?
DSA Javascript
const nums = [1, 2, 3]; const subsets = []; for (let mask = 0; mask < (1 << nums.length); mask++) { const subset = []; let sum = 0; for (let i = 0; i < nums.length; i++) { if (mask & (1 << i)) { subset.push(nums[i]); sum += nums[i]; } } if (sum > 3) { subsets.push(subset); } } console.log(subsets);
Attempts:
2 left
💡 Hint
Calculate sums of subsets and check which are greater than 3.
✗ Incorrect
Subsets with sum > 3 are [1,3] (sum 4), [2,3] (sum 5), and [1,2,3] (sum 6). Single element [3] sum is 3, not greater.
🧠 Conceptual
expertMaximum subsets generated by bitmask for n elements
For an array of length n, what is the maximum number of subsets generated using bitmask technique?
Attempts:
2 left
💡 Hint
Think about how each element can be either included or excluded independently.
✗ Incorrect
Each element has 2 choices: include or exclude. So total subsets are 2 multiplied by itself n times = 2^n.
