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 TypeScript code that generates all subsets of [1, 2] using bitmask?
DSA Typescript
const nums = [1, 2]; const subsets: number[][] = []; for (let mask = 0; mask < (1 << nums.length); mask++) { const subset: number[] = []; for (let i = 0; i < nums.length; i++) { if ((mask & (1 << i)) !== 0) { subset.push(nums[i]); } } subsets.push(subset); } console.log(subsets);
Attempts:
2 left
💡 Hint
Think about how bitmask from 0 to 2^n - 1 represents subsets.
✗ Incorrect
The bitmask runs from 0 to 3 (2^2 - 1). For each bitmask, bits set to 1 indicate inclusion of the element at that index. So 0 (00) means empty subset, 1 (01) means [1], 2 (10) means [2], and 3 (11) means [1, 2].
❓ Predict Output
intermediateNumber of subsets generated by bitmask
How many subsets will be generated by the bitmask approach for an input array of length 4?
DSA Typescript
const nums = [10, 20, 30, 40]; const subsets: number[][] = []; for (let mask = 0; mask < (1 << nums.length); mask++) { const subset: number[] = []; for (let i = 0; i < nums.length; i++) { if ((mask & (1 << i)) !== 0) { subset.push(nums[i]); } } subsets.push(subset); } console.log(subsets.length);
Attempts:
2 left
💡 Hint
Number of subsets of a set with n elements is 2^n.
✗ Incorrect
For an array of length 4, the number of subsets is 2^4 = 16.
🔧 Debug
advancedIdentify the error in subset generation code
What error will the following TypeScript code produce when generating subsets using bitmask?
DSA Typescript
const nums = [5, 10, 15]; const subsets: number[][] = []; for (let mask = 0; mask <= (1 << nums.length); mask++) { const subset: number[] = []; for (let i = 0; i < nums.length; i++) { if ((mask & (1 << i)) !== 0) { subset.push(nums[i]); } } subsets.push(subset); } console.log(subsets.length);
Attempts:
2 left
💡 Hint
Check the loop condition for mask variable.
✗ Incorrect
The loop runs from 0 to 8 inclusive, but 1 << 3 = 8, so mask goes up to 8 which is out of range for subsets of length 3. This causes one extra subset to be generated.
❓ Predict Output
advancedOutput of subsets with filtering condition
What is the output of the following TypeScript code that generates subsets of [1, 2, 3] but only includes subsets with sum greater than 3?
DSA Typescript
const nums = [1, 2, 3]; const subsets: number[][] = []; for (let mask = 0; mask < (1 << nums.length); mask++) { const subset: number[] = []; let sum = 0; for (let i = 0; i < nums.length; i++) { if ((mask & (1 << i)) !== 0) { subset.push(nums[i]); sum += nums[i]; } } if (sum > 3) { subsets.push(subset); } } console.log(subsets);
Attempts:
2 left
💡 Hint
Calculate sums of all subsets and check which are > 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 > 3, so excluded.
🧠 Conceptual
expertMaximum subsets generated by bitmask for n elements
For an input array of length n, what is the maximum number of subsets generated using the bitmask approach?
Attempts:
2 left
💡 Hint
Recall the total number of subsets of a set.
✗ Incorrect
The total number of subsets of a set with n elements is 2^n, which is the number of bitmasks from 0 to 2^n - 1.
