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];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable name instead of n.
Using multiplication instead of bit shift.
✗ Incorrect
Using bit shift operator 1 << n calculates 2^n, which is the total number of subsets.
2fill in blank
mediumComplete the for loop to iterate over all subsets represented by bitmasks.
DSA C
for (int mask = 0; mask < (1 << n); mask[1]) {
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using decrement operator which causes an infinite loop.
Using multiplication or addition incorrectly.
✗ Incorrect
The loop increments mask by 1 each time to cover all subsets from 0 to 2^n - 1.
3fill in blank
hardFix the error in the condition to check if the j-th element is included in the current subset mask.
DSA C
if (mask & (1 [1] j)) {
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using right shift instead of left shift.
Using bitwise OR or AND incorrectly.
✗ Incorrect
To check if the j-th bit is set, shift 1 left by j positions and bitwise AND with mask.
4fill in blank
hardFill both blanks to print all elements of the current subset represented by mask.
DSA C
for (int j = 0; j [1] n; j[2]) { if (mask & (1 << j)) { printf("%d ", arr[j]); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= instead of < causing out-of-bounds access.
Using decrement operator causing infinite loop.
✗ Incorrect
The loop runs from j = 0 up to j < n, incrementing j each time to check all elements.
5fill in blank
hardFill all three blanks to create a function that generates all subsets of an array using bitmask.
DSA C
void generate_subsets(int arr[], int n) {
int total = 1 [1] n;
for (int mask = 0; mask [2] total; mask++) {
printf("{ ");
for (int j = 0; j [3] n; j++) {
if (mask & (1 << j)) {
printf("%d ", arr[j]);
}
}
printf("}\n");
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= in loop conditions causing out-of-bounds errors.
Using wrong bit shift direction.
Not incrementing loop variables properly.
✗ Incorrect
Use bit shift to calculate total subsets, loop mask from 0 to total-1, and increment j to check each element.
