Bloom Filters & Probabilistic Data Structures for Interviews
Bloom filters answer "is this item possibly in the set?" with minimal space. They're essential for systems where false positives are acceptable but false negatives are not.
How Bloom Filters Work
A Bloom filter is a bit array of m bits and k hash functions. To add an item:
- Hash the item with all k functions
- Set the bits at all k positions to 1
To query an item:
- Hash with all k functions
- If any bit is 0, the item is definitely not in the set
- If all bits are 1, the item is probably in the set
Implementation
import mmh3
from bitarray import bitarray
class BloomFilter:
def __init__(self, size, hash_count):
self.size = size
self.hash_count = hash_count
self.bit_array = bitarray(size)
self.bit_array.setall(0)
def add(self, item):
for i in range(self.hash_count):
index = mmh3.hash(str(item), i) % self.size
self.bit_array[index] = 1
def check(self, item):
for i in range(self.hash_count):
index = mmh3.hash(str(item), i) % self.size
if not self.bit_array[index]:
return False
return True
Optimal Parameters
For n items and desired false positive rate p:
m = -n * ln(p) / (ln(2))² # Optimal size
k = m / n * ln(2) # Optimal hash count
import math
def optimal_params(n, p):
m = int(-n * math.log(p) / (math.log(2) ** 2))
k = int(m / n * math.log(2))
return m, k
Counting Bloom Filter
Standard Bloom filters don't support deletion. Counting Bloom filters replace bits with counters:
class CountingBloomFilter:
def __init__(self, size, hash_count):
self.size = size
self.hash_count = hash_count
self.counters = [0] * size
def add(self, item):
for i in range(self.hash_count):
index = mmh3.hash(str(item), i) % self.size
self.counters[index] += 1
def remove(self, item):
if not self.check(item):
return False
for i in range(self.hash_count):
index = mmh3.hash(str(item), i) % self.size
self.counters[index] -= 1
return True
def check(self, item):
for i in range(self.hash_count):
index = mmh3.hash(str(item), i) % self.size
if self.counters[index] == 0:
return False
return True
Applications
- Web caches: Avoid disk lookups for non-existent pages
- Databases: Bloom filters on SSTables (LevelDB, Cassandra)
- Spell checkers: Quick rejection of unknown words
- Network routing: Avoid routing loops
Other Probabilistic Structures
HyperLogLog
Estimate cardinality (unique count) in O(log log n) space:
class HyperLogLog:
def __init__(self, precision=14):
self.precision = precision
self.registers = [0] * (1 << precision)
def add(self, item):
h = mmh3.hash64(str(item))[0]
index = h & ((1 << self.precision) - 1)
h >>= self.precision
self.registers[index] = max(self.registers[index],
self._count_leading_zeros(h) + 1)
def _count_leading_zeros(self, x):
if x == 0:
return 64
count = 0
while (x & (1 << 63)) == 0:
x <<= 1
count += 1
return count
Interview Tips
- Clarify requirements. Bloom filters trade accuracy for space—make sure that's acceptable.
- Discuss alternatives. Hash sets for exact membership, tries for prefix queries.
- Common follow-ups: "How to resize?" "Handle deletion?" "Distributed Bloom filters?"
- Real examples. Google Bigtable, Apache Cassandra, Reddit's URL deduplication.
Ready to practice for your next interview? Interview Simulator gives you AI-powered feedback on your responses — behavioral, technical, and system design. Start with 3 free practice interviews today.
Try Interview Simulator free →
Master the most common coding interview patterns to ace your technical rounds.
For technical interview preparation, our system design guide is essential reading.
Don't neglect behavioral preparation — see our behavioral interview guide.
Your resume is the first impression — get it right with our resume guide.
{
"@context": "https://schema.org",
"@type": "BlogPosting",
"headline": "Bloom Filters & Probabilistic Data Structures for Interviews",
"description": "Understand Bloom filters, counting filters, and probabilistic data structures—when to trade accuracy for space efficiency.",
"datePublished": "2026-03-21",
"author": {
"@type": "Organization",
"name": "CodeSwiftr Team"
},
"url": "https://codeswiftr.com/blog/bloom-filters-probabilistic-ds"
}
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": [
{
"@type": "Question",
"name": "How should I structure my technical interview preparation?",
"acceptedAnswer": {
"@type": "Answer",
"text": "A structured 8–12 week preparation plan is most effective. Week 1–4: data structures and algorithms fundamentals using LeetCode easy-to-medium problems. Week 5–7: system design patterns using resources like the System Design Primer. Week 8–10: behavioral interview preparation with the STAR method. Week 11–12: company-specific mock interviews and review. Consistency matters more than intensity — 1–2 hours per day beats irregular all-day sessions."
}
},
{
"@type": "Question",
"name": "What is the STAR method and how should I use it in interviews?",
"acceptedAnswer": {
"@type": "Answer",
"text": "STAR stands for Situation, Task, Action, Result. It is the standard framework for answering behavioral interview questions. Situation: briefly describe the context (1–2 sentences). Task: explain your specific responsibility. Action: detail the steps you personally took — use 'I' not 'we'. Result: quantify the outcome wherever possible (e.g., 'reduced latency by 40%', 'increased conversion rate by 12%'). Keep each answer to 2–3 minutes."
}
},
{
"@type": "Question",
"name": "What LeetCode difficulty level should I focus on?",
"acceptedAnswer": {
"@type": "Answer",
"text": "For FAANG interviews, focus 20% on easy, 60% on medium, and 20% on hard problems. Easy problems build speed and confidence. Medium problems represent the most common interview difficulty at top companies. Hard problems appear mostly at Google, Meta, and specialised algorithmic roles. Do not skip easy problems — many candidates fail by overthinking genuinely simple questions that require clean, efficient solutions."
}
},
{
"@type": "Question",
"name": "How important is system design compared to coding in tech interviews?",
"acceptedAnswer": {
"@type": "Answer",
"text": "At mid-level and senior positions, system design carries equal or greater weight than coding. Entry-level and new-grad interviews are predominantly coding-focused. Senior and staff-level interviews dedicate one or two full rounds to system design. Master the core components: load balancers, databases (SQL vs NoSQL trade-offs), caches (Redis, Memcached), message queues (Kafka, SQS), CDNs, and API design. Practice designing real systems you use daily — the interviewer values practical reasoning over textbook answers."
}
}
]
}
Explore Related Topics
- Data Structures & Algorithms Cheat Sheet: Time...
- Data Structures Interview Cheatsheet: Complexity, Use...
- Data Structures Interview Review: Arrays, Trees, Graphs,...