Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the total number of subsets using bitmask.
DSA Typescript
const n = nums.length; const totalSubsets = 1 [1] n;
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' or '*' instead of bit shift to calculate total subsets.
Using '-' which gives wrong result.
✗ Incorrect
Using bit shift operator '<<' shifts 1 by n bits to get 2^n, which is total subsets count.
2fill in blank
mediumComplete the code to check if the j-th bit is set in the bitmask i.
DSA Typescript
if ((i & (1 [1] j)) !== 0) { subset.push(nums[j]); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>>' which shifts bits right, not useful here.
Using '&' or '|' inside the shift operation incorrectly.
✗ Incorrect
We use 1 << j to create a mask with only the j-th bit set, then & with i to check if that bit is set.
3fill in blank
hardFix the error in the code that generates subsets using bitmask.
DSA Typescript
for (let i = 0; i < [1]; i++) { const subset = []; for (let j = 0; j < n; j++) { if ((i & (1 << j)) !== 0) { subset.push(nums[j]); } } result.push(subset); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using n or nums.length as loop limit, which is too small.
Using result.length which changes dynamically.
✗ Incorrect
The outer loop should run from 0 to totalSubsets (2^n) to cover all subsets.
4fill in blank
hardFill both blanks to complete the function that returns all subsets using bitmask.
DSA Typescript
function subsets(nums: number[]): number[][] {
const n = nums.length;
const result: number[][] = [];
const total = 1 [1] n;
for (let i = 0; i < total; i++) {
const subset: number[] = [];
for (let j = 0; j < n; j++) {
if ((i & (1 [2] j)) !== 0) {
subset.push(nums[j]);
}
}
result.push(subset);
}
return result;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using right shift '>>' which is incorrect here.
Using bitwise AND '&' inside the shift operation.
✗ Incorrect
Use '<<' to calculate total subsets and to shift 1 by j bits to check bit set.
5fill in blank
hardFill all three blanks to complete the code that generates subsets using bitmask with filtering subsets of size greater than 1.
DSA Typescript
function filteredSubsets(nums: number[]): number[][] {
const n = nums.length;
const result: number[][] = [];
const total = 1 [1] n;
for (let i = 0; i < total; i++) {
const subset: number[] = [];
for (let j = 0; j < n; j++) {
if ((i & (1 [2] j)) !== 0) {
subset.push(nums[j]);
}
}
if (subset.length [3] 1) {
result.push(subset);
}
}
return result;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' for filtering subsets.
Using right shift '>>' which is incorrect here.
✗ Incorrect
Use '<<' for bit shifts and '>' to filter subsets with length greater than 1.
