Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
A Bitset is a data structure that compactly stores bits.
Implement the Bitset class:
Bitset(int size) Initializes the Bitset with size bits, all of which are 0.void fix(int idx) Updates the value of the bit at the index idx to 1. If the value was already 1, no change occurs.void unfix(int idx) Updates the value of the bit at the index idx to 0. If the value was already 0, no change occurs.void flip() Flips the values of each bit in the Bitset. In other words, all bits with value 0 will now have value 1 and vice versa.boolean all() Checks if the value of each bit in the Bitset is 1. Returns true if it satisfies the condition, false otherwise.boolean one() Checks if there is at least one bit in the Bitset with value 1. Returns true if it satisfies the condition, false otherwise.int count() Returns the total number of bits in the Bitset which have value 1.String toString() Returns the current composition of the Bitset. Note that in the resultant string, the character at the ith index should coincide with the value at the ith bit of the Bitset.Example 1:
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, 2, "01010"] Explanation Bitset bs = new Bitset(5); // bitset = "00000". bs.fix(3); // the value at idx = 3 is updated to 1, so bitset = "00010". bs.fix(1); // the value at idx = 1 is updated to 1, so bitset = "01010". bs.flip(); // the value of each bit is flipped, so bitset = "10101". bs.all(); // return False, as not all values of the bitset are 1. bs.unfix(0); // the value at idx = 0 is updated to 0, so bitset = "00101". bs.flip(); // the value of each bit is flipped, so bitset = "11010". bs.one(); // return True, as there is at least 1 index with value 1. bs.unfix(0); // the value at idx = 0 is updated to 0, so bitset = "01010". bs.count(); // return 2, as there are 2 bits with value 1. bs.toString(); // return "01010", which is the composition of bitset.
Constraints:
1 <= size <= 1050 <= idx <= size - 1105 calls will be made in total to fix, unfix, flip, all, one, count, and toString.all, one, count, or toString.5 calls will be made to toString.Problem summary: A Bitset is a data structure that compactly stores bits. Implement the Bitset class: Bitset(int size) Initializes the Bitset with size bits, all of which are 0. void fix(int idx) Updates the value of the bit at the index idx to 1. If the value was already 1, no change occurs. void unfix(int idx) Updates the value of the bit at the index idx to 0. If the value was already 0, no change occurs. void flip() Flips the values of each bit in the Bitset. In other words, all bits with value 0 will now have value 1 and vice versa. boolean all() Checks if the value of each bit in the Bitset is 1. Returns true if it satisfies the condition, false otherwise. boolean one() Checks if there is at least one bit in the Bitset with value 1. Returns true if it satisfies the condition, false otherwise. int count() Returns the total number of bits in the Bitset which have value 1. String toString() Returns
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Design
["Bitset","fix","fix","flip","all","unfix","flip","one","unfix","count","toString"] [[5],[3],[1],[],[],[0],[],[],[0],[],[]]
design-underground-system)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2166: Design Bitset
class Bitset {
private char[] a;
private char[] b;
private int cnt;
public Bitset(int size) {
a = new char[size];
b = new char[size];
Arrays.fill(a, '0');
Arrays.fill(b, '1');
}
public void fix(int idx) {
if (a[idx] == '0') {
a[idx] = '1';
++cnt;
}
b[idx] = '0';
}
public void unfix(int idx) {
if (a[idx] == '1') {
a[idx] = '0';
--cnt;
}
b[idx] = '1';
}
public void flip() {
char[] t = a;
a = b;
b = t;
cnt = a.length - cnt;
}
public boolean all() {
return cnt == a.length;
}
public boolean one() {
return cnt > 0;
}
public int count() {
return cnt;
}
public String toString() {
return String.valueOf(a);
}
}
/**
* Your Bitset object will be instantiated and called as such:
* Bitset obj = new Bitset(size);
* obj.fix(idx);
* obj.unfix(idx);
* obj.flip();
* boolean param_4 = obj.all();
* boolean param_5 = obj.one();
* int param_6 = obj.count();
* String param_7 = obj.toString();
*/
// Accepted solution for LeetCode #2166: Design Bitset
type Bitset struct {
a []byte
b []byte
cnt int
}
func Constructor(size int) Bitset {
a := bytes.Repeat([]byte{'0'}, size)
b := bytes.Repeat([]byte{'1'}, size)
return Bitset{a, b, 0}
}
func (this *Bitset) Fix(idx int) {
if this.a[idx] == '0' {
this.a[idx] = '1'
this.cnt++
}
this.b[idx] = '0'
}
func (this *Bitset) Unfix(idx int) {
if this.a[idx] == '1' {
this.a[idx] = '0'
this.cnt--
}
this.b[idx] = '1'
}
func (this *Bitset) Flip() {
this.a, this.b = this.b, this.a
this.cnt = len(this.a) - this.cnt
}
func (this *Bitset) All() bool {
return this.cnt == len(this.a)
}
func (this *Bitset) One() bool {
return this.cnt > 0
}
func (this *Bitset) Count() int {
return this.cnt
}
func (this *Bitset) ToString() string {
return string(this.a)
}
/**
* Your Bitset object will be instantiated and called as such:
* obj := Constructor(size);
* obj.Fix(idx);
* obj.Unfix(idx);
* obj.Flip();
* param_4 := obj.All();
* param_5 := obj.One();
* param_6 := obj.Count();
* param_7 := obj.ToString();
*/
# Accepted solution for LeetCode #2166: Design Bitset
class Bitset:
def __init__(self, size: int):
self.a = ['0'] * size
self.b = ['1'] * size
self.cnt = 0
def fix(self, idx: int) -> None:
if self.a[idx] == '0':
self.a[idx] = '1'
self.cnt += 1
self.b[idx] = '0'
def unfix(self, idx: int) -> None:
if self.a[idx] == '1':
self.a[idx] = '0'
self.cnt -= 1
self.b[idx] = '1'
def flip(self) -> None:
self.a, self.b = self.b, self.a
self.cnt = len(self.a) - self.cnt
def all(self) -> bool:
return self.cnt == len(self.a)
def one(self) -> bool:
return self.cnt > 0
def count(self) -> int:
return self.cnt
def toString(self) -> str:
return ''.join(self.a)
# Your Bitset object will be instantiated and called as such:
# obj = Bitset(size)
# obj.fix(idx)
# obj.unfix(idx)
# obj.flip()
# param_4 = obj.all()
# param_5 = obj.one()
# param_6 = obj.count()
# param_7 = obj.toString()
// Accepted solution for LeetCode #2166: Design Bitset
/**
* [2166] Design Bitset
*
* A Bitset is a data structure that compactly stores bits.
* Implement the Bitset class:
*
* Bitset(int size) Initializes the Bitset with size bits, all of which are 0.
* void fix(int idx) Updates the value of the bit at the index idx to 1. If the value was already 1, no change occurs.
* void unfix(int idx) Updates the value of the bit at the index idx to 0. If the value was already 0, no change occurs.
* void flip() Flips the values of each bit in the Bitset. In other words, all bits with value 0 will now have value 1 and vice versa.
* boolean all() Checks if the value of each bit in the Bitset is 1. Returns true if it satisfies the condition, false otherwise.
* boolean one() Checks if there is at least one bit in the Bitset with value 1. Returns true if it satisfies the condition, false otherwise.
* int count() Returns the total number of bits in the Bitset which have value 1.
* String toString() Returns the current composition of the Bitset. Note that in the resultant string, the character at the i^th index should coincide with the value at the i^th bit of the Bitset.
*
*
* Example 1:
*
* 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, 2, "01010"]
* Explanation
* Bitset bs = new Bitset(5); // bitset = "00000".
* bs.fix(3); // the value at idx = 3 is updated to 1, so bitset = "00010".
* bs.fix(1); // the value at idx = 1 is updated to 1, so bitset = "01010".
* bs.flip(); // the value of each bit is flipped, so bitset = "10101".
* bs.all(); // return False, as not all values of the bitset are 1.
* bs.unfix(0); // the value at idx = 0 is updated to 0, so bitset = "00101".
* bs.flip(); // the value of each bit is flipped, so bitset = "11010".
* bs.one(); // return True, as there is at least 1 index with value 1.
* bs.unfix(0); // the value at idx = 0 is updated to 0, so bitset = "01010".
* bs.count(); // return 2, as there are 2 bits with value 1.
* bs.toString(); // return "01010", which is the composition of bitset.
*
*
* Constraints:
*
* 1 <= size <= 10^5
* 0 <= idx <= size - 1
* At most 10^5 calls will be made in total to fix, unfix, flip, all, one, count, and toString.
* At least one call will be made to all, one, count, or toString.
* At most 5 calls will be made to toString.
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/design-bitset/
// discuss: https://leetcode.com/problems/design-bitset/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
// Credit: https://leetcode.com/problems/design-bitset/solutions/4344356/rust-bitset/
struct Bitset {
bitsets: Vec<u32>,
last_bitset_mask: u32,
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl Bitset {
fn new(size: i32) -> Self {
Self {
bitsets: vec![0; (size as usize + 31) / 32],
last_bitset_mask: (1 << 32 - size % 32) - 1,
}
}
fn fix(&mut self, idx: i32) {
self.bitsets[idx as usize / 32] |= 1 << 31 - idx % 32;
}
fn unfix(&mut self, idx: i32) {
self.bitsets[idx as usize / 32] &= !(1 << 31 - idx % 32);
}
fn flip(&mut self) {
for bitset in &mut self.bitsets {
*bitset = !*bitset;
}
}
fn all(&self) -> bool {
self.bitsets[..self.bitsets.len() - 1]
.iter()
.all(|&bitset| bitset == u32::MAX)
&& self.bitsets[self.bitsets.len() - 1] | self.last_bitset_mask == u32::MAX
}
fn one(&self) -> bool {
self.bitsets[..self.bitsets.len() - 1]
.iter()
.any(|&bitset| bitset > 0)
|| self.bitsets[self.bitsets.len() - 1] & !(self.last_bitset_mask) > 0
}
fn count(&self) -> i32 {
self.bitsets[..self.bitsets.len() - 1]
.iter()
.map(|bitset| bitset.count_ones() as i32)
.sum::<i32>()
+ (self.bitsets[self.bitsets.len() - 1] & !(self.last_bitset_mask)).count_ones() as i32
}
fn to_string(&self) -> String {
let mut out = String::with_capacity(self.bitsets.len() * 32);
for &bitset in &self.bitsets[..self.bitsets.len() - 1] {
std::fmt::Write::write_fmt(&mut out, format_args!("{:032b}", bitset));
}
std::fmt::Write::write_fmt(
&mut out,
format_args!("{:032b}", self.bitsets[self.bitsets.len() - 1]),
);
out.truncate(out.len() - self.last_bitset_mask.count_ones() as usize);
out
}
}
/**
* Your Bitset object will be instantiated and called as such:
* let obj = Bitset::new(size);
* obj.fix(idx);
* obj.unfix(idx);
* obj.flip();
* let ret_4: bool = obj.all();
* let ret_5: bool = obj.one();
* let ret_6: i32 = obj.count();
* let ret_7: String = obj.to_string();
*/
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_2166_example_1() {
let mut bs = Bitset::new(5); // bitset = "00000".
bs.fix(3); // the value at idx = 3 is updated to 1, so bitset = "00010".
bs.fix(1); // the value at idx = 1 is updated to 1, so bitset = "01010".
bs.flip(); // the value of each bit is flipped, so bitset = "10101".
assert_eq!(bs.all(), false); // return False, as not all values of the bitset are 1.
bs.unfix(0); // the value at idx = 0 is updated to 0, so bitset = "00101".
bs.flip(); // the value of each bit is flipped, so bitset = "11010".
assert_eq!(bs.one(), true); // return True, as there is at least 1 index with value 1.
bs.unfix(0); // the value at idx = 0 is updated to 0, so bitset = "01010".
assert_eq!(bs.count(), 2); // return 2, as there are 2 bits with value 1.
assert_eq!(bs.to_string(), "01010".to_string()); // return "01010", which is the composition of bitset.
}
}
// Accepted solution for LeetCode #2166: Design Bitset
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #2166: Design Bitset
// class Bitset {
// private char[] a;
// private char[] b;
// private int cnt;
//
// public Bitset(int size) {
// a = new char[size];
// b = new char[size];
// Arrays.fill(a, '0');
// Arrays.fill(b, '1');
// }
//
// public void fix(int idx) {
// if (a[idx] == '0') {
// a[idx] = '1';
// ++cnt;
// }
// b[idx] = '0';
// }
//
// public void unfix(int idx) {
// if (a[idx] == '1') {
// a[idx] = '0';
// --cnt;
// }
// b[idx] = '1';
// }
//
// public void flip() {
// char[] t = a;
// a = b;
// b = t;
// cnt = a.length - cnt;
// }
//
// public boolean all() {
// return cnt == a.length;
// }
//
// public boolean one() {
// return cnt > 0;
// }
//
// public int count() {
// return cnt;
// }
//
// public String toString() {
// return String.valueOf(a);
// }
// }
//
// /**
// * Your Bitset object will be instantiated and called as such:
// * Bitset obj = new Bitset(size);
// * obj.fix(idx);
// * obj.unfix(idx);
// * obj.flip();
// * boolean param_4 = obj.all();
// * boolean param_5 = obj.one();
// * int param_6 = obj.count();
// * String param_7 = obj.toString();
// */
Use this to step through a reusable interview workflow for this problem.
Use a simple list or array for storage. Each operation (get, put, remove) requires a linear scan to find the target element — O(n) per operation. Space is O(n) to store the data. The linear search makes this impractical for frequent operations.
Design problems target O(1) amortized per operation by combining data structures (hash map + doubly-linked list for LRU, stack + min-tracking for MinStack). Space is always at least O(n) to store the data. The challenge is achieving constant-time operations through clever structure composition.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.