Design Bitset - Problem
Design a Bitset Data Structure
A
Your task is to implement a
Challenge: The
A
Bitset is a memory-efficient data structure that stores a collection of bits (0s and 1s). Think of it as a compact array of boolean values where each position can be either on (1) or off (0).Your task is to implement a
Bitset class that supports the following operations:Bitset(int size)- Initialize withsizebits, all set to 0void fix(int idx)- Set the bit at indexidxto 1void unfix(int idx)- Set the bit at indexidxto 0void flip()- Flip all bits (0โ1, 1โ0)boolean all()- Check if all bits are 1boolean one()- Check if at least one bit is 1int count()- Count how many bits are 1String toString()- Return string representation
Challenge: The
flip() operation should be efficient even for large bitsets! Input & Output
example_1.py โ Basic Operations
$
Input:
["Bitset", "fix", "fix", "flip", "all", "unfix", "flip", "one", "unfix", "count", "toString"]
[[5], [3], [1], [], [], [0], [], [], [0], [], []]
โบ
Output:
[null, null, null, null, false, null, null, true, null, 4, "11011"]
๐ก Note:
Initialize bitset of size 5: "00000" โ fix(3): "00010" โ fix(1): "01010" โ flip(): "10101" โ all(): false (not all 1s) โ unfix(0): "00101" โ flip(): "11010" โ one(): true (has 1s) โ unfix(0): "01010" โ count(): 3 ones โ toString(): "01010"
example_2.py โ Flip Performance
$
Input:
["Bitset", "flip", "flip", "fix", "flip", "count"]
[[3], [], [], [1], [], []]
โบ
Output:
[null, null, null, null, null, 2]
๐ก Note:
Initialize: "000" โ flip(): "111" โ flip(): "000" โ fix(1): "010" โ flip(): "101" โ count(): 2 ones. Shows how multiple flips are handled efficiently.
example_3.py โ Edge Case Single Bit
$
Input:
["Bitset", "fix", "all", "flip", "all", "toString"]
[[1], [0], [], [], [], []]
โบ
Output:
[null, null, true, null, false, "0"]
๐ก Note:
Single bit bitset: Initialize "0" โ fix(0): "1" โ all(): true โ flip(): "0" โ all(): false โ toString(): "0"
Visualization
Tap to expand
Understanding the Visualization
1
Initialize
Start with all lights off (bits = 0) and normal mode (flipped = false)
2
Fix/Unfix
Manually change individual switches, accounting for current building mode
3
Flip
Instead of changing every switch, just toggle the building mode
4
Query
To check actual light status, apply building mode to switch positions
Key Takeaway
๐ฏ Key Insight: The lazy flip optimization transforms an O(n) operation into O(1) by changing how we interpret data rather than modifying it directly
Time & Space Complexity
Time Complexity
O(1) for all operations
Flip is O(1) due to lazy evaluation, queries use cached count
โ Linear Growth
Space Complexity
O(n)
Still need O(n) space for bits plus O(1) for flip flag and counters
โก Linearithmic Space
Constraints
- 1 โค size โค 105
- 0 โค idx < size
- At most 105 calls will be made to fix, unfix, flip, all, one, count, and toString
- Performance requirement: flip() should be efficient for large bitsets
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code