Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to generate all subsets of a list using bitmask.
DSA Python
nums = [1, 2, 3] subsets = [] n = len(nums) for mask in range(1 << n): subset = [] for i in range(n): if mask & (1 << [1]): subset.append(nums[i]) subsets.append(subset) print(subsets)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using mask instead of i in the bit shift.
Using n or subset instead of i for the bit position.
✗ Incorrect
We use 'i' to check each bit position in the mask to decide if nums[i] is included in the subset.
2fill in blank
mediumComplete the code to calculate the total number of subsets using bitmask.
DSA Python
nums = [4, 5, 6, 7] n = len(nums) total_subsets = [1] print(total_subsets)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication instead of bit shift.
Using n squared instead of 2 to the power n.
✗ Incorrect
Total subsets of a set with n elements is 2^n, which is 1 shifted left by n bits.
3fill in blank
hardFix the error in the code to correctly generate subsets using bitmask.
DSA Python
nums = [8, 9] n = len(nums) subsets = [] for mask in range(1 << n): subset = [] for i in range(n): if mask & (1 << i): subset.append(nums[[1]]) subsets.append(subset) print(subsets)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Appending nums[mask] instead of nums[i].
Using wrong variable in bit shift.
✗ Incorrect
We must use 'i' to index nums, not 'mask', to get the correct element for the subset.
4fill in blank
hardFill both blanks to create a list of subsets with their sums using bitmask.
DSA Python
nums = [1, 2, 3] n = len(nums) subsets_with_sum = [] for mask in range(1 << n): subset = [] for i in range(n): if mask & (1 << [1]): subset.append(nums[i]) subsets_with_sum.append((subset, sum([2]))) print(subsets_with_sum)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using nums instead of subset for sum.
Using mask or i instead of subset for sum.
✗ Incorrect
Use 'i' to check bits and 'subset' to calculate the sum of elements included.
5fill in blank
hardFill all three blanks to create a dictionary mapping subsets (as tuples) to their sums using bitmask.
DSA Python
nums = [2, 4] n = len(nums) subset_sum_map = {} for mask in range(1 << n): subset = [] for i in range(n): if mask & (1 << [1]): subset.append(nums[i]) subset_sum_map[tuple([2])] = [3] print(subset_sum_map)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using nums instead of subset for key.
Using subset instead of sum(subset) for value.
Using wrong variable for bit check.
✗ Incorrect
Use 'i' for bit check, 'subset' as key (converted to tuple), and sum(subset) as value.