Skip to interview prep
Apexed

What is a Bloom filter and what are its trade-offs?

MediumSymbol tables & hashing

Technical

Content review

Report an error

Not yet reviewed

Getting your answer ready…

Show model answer

A Bloom filter is a bit array of m bits with k independent hash functions. Adding an item sets the k bits it hashes to; a query checks those k bits. If any is 0, the item was definitely never added; if all are 1, it was probably added. So there are no false negatives, but false positives occur, with probability about (1 − e^(−kn/m))^k after n insertions; the optimal k is (m/n)·ln 2.

It uses a tiny fraction of the memory of a hash set (about 10 bits per item for 1% false positives) and supports O(k) insert and query. You cannot list or delete items (counting Bloom filters allow deletion at extra cost). Typical uses: avoiding disk lookups for absent keys in databases, caches and web crawlers.

Example program

What is a Bloom filter and what are its trade-offs — Data structures & algorithms interview question — Apexed