Vibe Engines
YouTube

Vibe Engines is free, interactive AI engineering & system design — runs in your browser. Pick a learning path

Algorithms · Probabilistic

Maybe Yes, Never No.

Watch the explainer

A Bloom filter answers one question — "have I seen this before?" — using a tiny bit array and no stored keys at all. Adding an item flips k bits chosen by k hashes. To check membership, look at those same k bits: if any is 0, the item is definitely not in the set; if all are 1, it's probably in — probably, because other items may have set those bits. It can give a false yes, but never a false no. Add a few items, then go hunting for a false positive.

O(k) add & query · O(m) bits, no keys stored · one-sided error
bit array

m bits, all starting at 0. The entire data structure — no keys are ever stored.

k hashes

Each item maps to k bit positions via k independent hash functions.

add

Set all k of the item’s bits to 1.

query

Any of the k bits is 0 → definitely absent. All 1 → probably present.

bloom.js — k hashes, m bits, no keys
Ready
A bit array of 24 bits, all zero. Each item is hashed by k=3 functions to three bit positions. Add item sets those bits; Query checks them.
0
items added
0/24
bits set
0%
est. FP rate

How it works

The magic is the asymmetry of the error. A "no" is always trustworthy: if even one of the item's k bits is 0, then this item was never added, because adding it would have set that bit. A "yes" is only probable: all k bits being 1 could be a coincidence — other items may have set exactly those bits. So the filter is a fast, tiny pre-check that can rule things out with certainty and rule them in only with high probability.

1

Start with all zeros

The bit array holds only bits — never the items themselves. That’s why it’s so small: it doesn’t store keys.

2

Add: light k bits

Run the item through k hashes to get k positions, and set all of them to 1. Bits may already be set by other items — that’s fine, and it’s the source of collisions.

3

Query: check k bits

Look at the item’s k positions. Any 0 means it was definitely never added. All 1 means probably added — but maybe those bits were set by others.

✓

Tune the false-positive rate

More bits (larger m) or more items (larger n) change the fill. The false-positive rate is about (1 − e^(−kn/m))^k — pick m and k for your target.

Add / query
O(k)
Space
O(m) bits
False negatives
Never
False positives
Tunable

The code

# m bits, k hash functions. No keys are ever stored. class BloomFilter: def __init__(self, m, k): self.m, self.k = m, k self.bits = [0] * m def add(self, item): for i in range(self.k): self.bits[hash_i(item, i) % self.m] = 1 # light k bits def __contains__(self, item): for i in range(self.k): if self.bits[hash_i(item, i) % self.m] == 0: return False # a 0 bit => DEFINITELY absent return True # all 1 => PROBABLY present

Quick check

1. A Bloom filter says an item is "not present". How sure can you be?

2. It says an item IS present. What does that mean?

3. Why is a Bloom filter so space-efficient?

FAQ

What is a Bloom filter?

A space-efficient probabilistic data structure for set membership. It uses a bit array and k hash functions: adding sets k bits, querying checks them. It can return false positives but never false negatives.

Why can a Bloom filter have false positives but not false negatives?

Adding only ever sets bits, never clears them. So a truly-added item always has all k bits at 1 — a query can’t miss it (no false negatives). But those bits may also be 1 from other items, so a never-added item can read as present (false positive).

Can you delete from a Bloom filter?

Not from a standard one — clearing a bit might break another item that shares it, causing false negatives. A counting Bloom filter uses small counters instead of bits to allow deletion, at the cost of more space.

Where are Bloom filters used?

As a fast pre-check to skip expensive work: LSM-tree databases (Cassandra, Bigtable) use them to avoid disk reads for absent keys; browsers/CDNs for malicious-URL checks; crawlers to skip seen URLs. Anywhere a cheap "definitely not here" saves an expensive lookup.

The picture: 24 bits, three hashes

001110410108100112001116001020110add "cat" and 5 more; then query two animals never addedwolfelkwolf: bits 12, 15, 2 are 1, 1, 1 → false positive elk: bits 8, 5, 4 are 0, 1, 0 → absent
The filter after six adds: 12 of 24 bits are 1 (green). One query lands entirely on set bits without ever having been added, which is the false positive; the other hits a zero and is rejected for certain. The filter holds only bits, so nothing in it says which items set them.

Worked example, step by step

Use m = 24 bits and k = 3 hashes. Hash i is FNV-1a over the characters, seeded with i + 1, finished with the murmur3 mixing steps, taken mod 24. Add cat, dog, fox, owl, ant, bee in that order, then query the other 18 animals from the page's pool, none of which was added. The table shows 1 of the animals that fool the filter, then 5 that are correctly rejected. Every position below comes from running that code.

StepItemBitsNewly setTotal ones
add 1cat3, 3, 222
add 2dog9, 19, 1924
add 3fox16, 15, 137
add 4owl16, 3, 2218
add 5ant1, 7, 219
add 6bee12, 5, 21312
querywolf12, 15, 21,1,1all 1: "probably yes"
queryelk8, 5, 40,1,0a 0: "no"
queryram17, 18, 150,0,1a 0: "no"
queryyak13, 0, 40,0,0a 0: "no"
queryjay15, 20, 201,0,0a 0: "no"
querycod1, 14, 121,0,1a 0: "no"

After 6 adds, 12 of 24 bits are set (50%). 1 of the 18 absent animals came back "probably yes" (5.56%). The textbook formula (1 − e−kn/m)k with n = 6 predicts 14.69%; 18 probes cannot confirm a rate, so the complexity section measures it at scale. The structure of the answer is what matters here. Every "no" (a 0 in any position) is certain, and every "yes" is only as good as the fill: the fuller the array, the likelier three random bits are all 1.

Complexity and trade-offs

CaseTimeSpaceWhy
Bloom filterO(k) add and queryO(m) bitsNo false negatives. False-positive rate about (1 − e−kn/m)k. No deletion, no listing.
Hash setO(1) expectedO(n · key size)Exact and can delete, but stores every key in full.
Sorted array + binary searchO(log n) queryO(n · key size)Exact, cache-friendly, expensive to insert into.
Counting Bloom filterO(k)O(m) countersAllows deletion; counters cost several times the space of bits.
Cuckoo filterO(1) expectedO(n) fingerprintsSupports deletion and often uses less space at low false-positive rates.

Measured with the hash above, 1,000 stored items and 20,000 probes for items that were never added. At m = 9,586 (about 9.6 bits per item) and k = 7: 0.88% false positives, 4933 bits set, against 1.00% from the formula. With k = 1: 10.14% (formula 9.91%). With k = 20: 7.19% (formula 7.06%), because too many hashes fill the array. Halving m to 4,793 at k = 7: 16.43% (formula 15.74%). More hashes help only until the array is half full; after that every extra hash is a net loss.

Common mistakes

  • Deleting by clearing bits

    Clearing the three bits of fox in the 24-bit filter above leaves owl, ant unable to be found: a false negative, which a Bloom filter promises never to give. Use a counting filter, or rebuild the filter without the item.

  • Using one hash for all k positions

    If every "hash" is the same function, the k bits collapse into one and the filter is a k = 1 filter. With the same 1,000 items and 9,586 bits that gives 10.14% false positives instead of 0.88%. Derive k functions from different seeds, or from two hashes combined as h1 + i·h2.

  • Inserting far more than you sized for

    Sized for 1,000 items (9,586 bits, k = 7), the filter holds 0.88% false positives. Push 5,000 items into it and the measured rate becomes 83.34% with 9333 of 9,586 bits set. The array does not refuse; it just degrades. Size from the item count you will really reach.

  • Reading "yes" as "present"

    In the 24-bit example, wolf was never added and still reads all ones. Use a Bloom filter to skip work on a "no", and confirm every "yes" against the real store.

  • Querying with different m or k than you built with

    A filter built with m = 9,586 and queried as if m were 9,587 loses items it holds: only 1.00% of the 1,000 stored items are still found, so 99.00% of them become false negatives. The bit array is meaningless without the exact m, k and hash that filled it, so store them with the filter.

Where this shows up

  • Skipping disk reads

    Storage engines that keep data in many sorted files can hold a small filter per file in memory. A "no" skips the file entirely; only a "yes" costs a disk read, so a false positive costs one wasted read and never a wrong answer.

  • Seen-before checks

    Crawlers and feed systems use a filter to skip URLs or ids they have probably handled. The occasional false positive means a page is skipped, which is acceptable when the alternative is storing every address.

  • Blocklists and caches

    A client can test a cheap local filter before asking a server whether a name is on a blocklist, or before looking up a key in a remote cache, and only contact the server on a "yes".

  • Set intersection and joins

    One side of a distributed join sends a filter of its keys to the other side, which drops rows that cannot match before shipping them across the network.

SOLVE IT YOURSELF

Solve it: build a Bloom filter

A set that never says "no" when it means "yes" — but sometimes says "yes" when it means "no". Build one and prove the one-sided error for yourself, in Python or TypeScript, running for real in your browser.

YOUR TASK

Implement make_filter(items, m, k) — return a list of m bits with k hash positions set per item — and might_contain(bits, item, k), which returns True only if all k bits for that item are set. Use the provided bloom_hash so both sides agree.

HINTS — 5 IDEAS
  1. For each item, set the bit at bloom_hash(item, seed, m) for every seed 1…k. Bits are only ever turned on — never off.
  2. A lookup returns true only if every one of those k bits is set. One zero bit is a definitive "no".
  3. That asymmetry is the whole design: a stored item always has its bits set, so false negatives are impossible.
  4. A false positive happens when other items happen to have set all k of your bits — which is why the filter shrinks m or grows k to trade error for space.
  5. Nothing here stores the items themselves, which is why a Bloom filter is tiny — and why you can never enumerate or delete from it.
CPython · WebAssembly

Keep going

▶  Watch it explained

Bloom Filter: Time and Space Complexity, From First Principles

Finished this one? 0 / 99 Algorithms done
NextCount–Min Sketch: Counting in Tiny Space Next on the “Trading exactness for memory” ladder · rung 2 of 4

Explore the topic

See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.

More Algorithms