Design Bitset - Problem
Design a Bitset Data Structure

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 with size bits, all set to 0
  • void fix(int idx) - Set the bit at index idx to 1
  • void unfix(int idx) - Set the bit at index idx to 0
  • void flip() - Flip all bits (0โ†’1, 1โ†’0)
  • boolean all() - Check if all bits are 1
  • boolean one() - Check if at least one bit is 1
  • int count() - Count how many bits are 1
  • String 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
Bitset: Smart Building Light Control SystemPhysical Switches0Pos 01Pos 10Pos 21Pos 30Pos 4Mode: INVERTEDInterpretActual Light Status1Light 0: ON0Light 1: OFF1Light 2: ON0Light 3: OFF1Light 4: ONFormula: switch XOR modeCount: 3 lights ONOperations & Time Complexity๐Ÿ”ง fix(idx): Manually set switch โ†’ O(1)๐Ÿ”“ unfix(idx): Manually clear switch โ†’ O(1)๐Ÿ”„ flip(): Toggle building mode โ†’ O(1)โœ… all(): Check if count == size โ†’ O(1)๐Ÿ‘๏ธ one(): Check if count > 0 โ†’ O(1)๐Ÿ”ข count(): Return cached count โ†’ O(1)๐Ÿ“ toString(): Build string โ†’ O(n)๐Ÿ’พ Space: Array + flags โ†’ O(n)๐ŸŽฏ Key Insight: Instead of O(n) flip, use O(1) mode toggle + lazy evaluationPerfect for applications with frequent flip operations!
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

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Still need O(n) space for bits plus O(1) for flip flag and counters

n
2n
โšก 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
Asked in
Google 42 Meta 35 Microsoft 28 Amazon 22
28.4K Views
Medium-High Frequency
~25 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen