0
0
DSA Pythonprogramming~10 mins

Subsets Generation Using Bitmask in DSA Python - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Ai
Bsubset
Cn
Dmask
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.
2fill in blank
medium

Complete 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'
A2 * n
Bn * 2
C1 << n
Dn ** 2
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication instead of bit shift.
Using n squared instead of 2 to the power n.
3fill in blank
hard

Fix 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'
Amask
Bi
Cn
Dsubset
Attempts:
3 left
💡 Hint
Common Mistakes
Appending nums[mask] instead of nums[i].
Using wrong variable in bit shift.
4fill in blank
hard

Fill 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'
Ai
Bsubset
Cnums
Dmask
Attempts:
3 left
💡 Hint
Common Mistakes
Using nums instead of subset for sum.
Using mask or i instead of subset for sum.
5fill in blank
hard

Fill 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'
Ai
Bsubset
Csum(subset)
Dnums
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.