Concurrent Bloom Filter
HardConcurrencyBloom FilterAtomicIntegerArrayLock-FreeProbabilisticBit Array
Problem Statement
Design a thread-safe, lock-free Bloom Filter — a space-efficient probabilistic data structure that tells you if an element is 'definitely not in the set' or 'possibly in the set'. Multiple threads must concurrently add elements and query membership without data corruption.
class ConcurrentBloomFilter<T> {
// n = expected elements, fpp = target false positive probability
public ConcurrentBloomFilter(int expectedElements, double falsePositiveProbability) {}
// Thread-safe add
public void add(T item) {}
// Returns false = definitely NOT in set; true = POSSIBLY in set
public boolean mightContain(T item) {}
// Approximate current element count
public long approximateElementCount() {}
// False positive rate estimate given current population
public double currentFalsePositiveProbability() {}
// Merge two filters (union)
public ConcurrentBloomFilter<T> merge(ConcurrentBloomFilter<T> other) {}
}
Key Entities
- AtomicIntegerArray bit array: Backing bit array where bits are set atomically using CAS on word-level AtomicIntegers.
- k hash functions: Typically k = (m/n) × ln(2) computed from desired FPP.
- m bits: Total bit array size = -n × ln(fpp) / (ln(2))².
- LongAdder elementCount: Tracks approximate insertions.
Requirements
add()must be lock-free — no mutexes.mightContain()must never return false negative (false negatives are forbidden).- False positive rate must be ≤ configured
falsePositiveProbabilitywhen element count ≤expectedElements. - Thread-safe for concurrent adds and queries.
- Explain the hash function choice (MurmurHash3 or double hashing recommended).
Asked at: Google, Amazon, Facebook, Akamai, Cassandra (Apache), LinkedIn
!Constraints
- 1 ≤ expectedElements ≤ 10^8
- 0.0001 ≤ falsePositiveProbability ≤ 0.5
- Lock-free: no synchronized blocks or ReentrantLock.
- Must use at least 3 hash functions (k ≥ 3).
- Bit array must be stored as AtomicIntegerArray (integers represent 32-bit words).
Output
Run or submit your code to see output here.