Challenge - 5 Problems
Bitmask Architect
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
easyBitmask and Subset Correspondence
For an array of 3 elements [a, b, c], which bitmask (in binary) represents the subset {a, c}?
Attempts:
2 left
💡 Hint
Assign bit 0 to element a (rightmost), bit 1 to b, bit 2 to c. A set bit means the element is included.
✗ Incorrect
With bit 0 → a, bit 1 → b, bit 2 → c: subset {a, c} includes a (bit 0 = 1) and c (bit 2 = 1) but not b (bit 1 = 0). Binary: 101. In decimal: 5. The bitmask approach uses each integer from 0 to 2^n - 1, with each bit position representing whether the corresponding element is included.
❓ Predict Output
intermediateBitmask Subset Count
What does this C# code print for nums = [1, 2, 3]?
DSA C#
int n = 3; int total = 1 << n; Console.WriteLine(total);
Attempts:
2 left
💡 Hint
1 << n shifts 1 left by n positions, equivalent to 2^n.
✗ Incorrect
1 << 3 = 2^3 = 8. This is the total number of subsets for a 3-element array. Bitmasks 0 through 7 (binary 000 to 111) each represent one unique subset. The loop to generate all subsets iterates mask from 0 to (1 << n) - 1.
🔧 Debug
intermediateFix the Bit Check
This C# code is supposed to check if bit j is set in mask, but it always returns true. What is the bug?
DSA C#
bool IsBitSet(int mask, int j) { return (mask | (1 << j)) != 0; // BUG }
Attempts:
2 left
💡 Hint
OR sets bits — it can never decrease a value to 0. What operation isolates a specific bit?
✗ Incorrect
mask | (1 << j) always sets bit j, making the result always non-zero — so it always returns true regardless of the original mask. The correct check uses AND: (mask & (1 << j)) != 0. AND isolates bit j — if it was already set in mask, the result is non-zero (true); if it wasn't set, the result is 0 (false).
🚀 Application
intermediateComplete the Bitmask Subset Generator
Which option correctly completes the inner loop to print elements for each subset?
DSA C#
int[] nums = {1, 2, 3}; int n = nums.Length; for (int mask = 0; mask < (1 << n); mask++) { var subset = new List<int>(); for (int j = 0; j < n; j++) { if (/* CONDITION */) { subset.Add(nums[j]); } } Console.WriteLine("[" + string.Join(",", subset) + "]"); }
Attempts:
2 left
💡 Hint
We want to check if bit j is set in mask — which bitwise operation isolates a specific bit?
✗ Incorrect
(mask & (1 << j)) != 0 checks whether bit j is set in mask. If set, element j is part of this subset and is added to the list. This correctly maps each bit position to its corresponding element: bit 0 → nums[0], bit 1 → nums[1], bit 2 → nums[2].
🧠 Conceptual
hardBitmask vs Backtracking Trade-off
Compared to the backtracking approach for subset generation, what is the key advantage of the bitmask approach?
Attempts:
2 left
💡 Hint
Think about recursion depth, stack frames, and how the bitmask approach iterates through subsets.
✗ Incorrect
The bitmask approach replaces recursion with a simple integer counter from 0 to 2^n - 1. No recursive function calls, no call stack growth, no risk of stack overflow for large n. The iteration order is deterministic (numeric order of masks). Both approaches generate exactly 2^n subsets in O(n × 2^n) total time — the time complexity is the same, but bitmask is simpler and more cache-friendly.
