Design a Bloom Filter
🌸 What is a Bloom filter? It is a tiny memory that answers one question: "have I seen this item before?" Instead of storing the items, it turns on a few bits for each one. When you ask about an item, it checks those bits. If any bit is off, the item was never added. If all are on, it was probably added, but it might be a coincidence.
Bloom filters show up wherever a system wants to skip expensive work cheaply: a web crawler that should not fetch the same URL twice, a database that wants to skip reading a disk file that cannot hold a key, a sign-up form that checks whether a username is taken before asking the server. In an interview, the problem is small, so the interviewer is looking for three things: a clean split between the bits and the hashing, a correct explanation of why false positives happen and false negatives never do, and the math to size the filter for a target error rate.
Requirements
"Design a Bloom filter. It should let us add strings and ask whether a string might have been added, using much less memory than storing the strings."
The interesting decisions are all hidden behind "much less memory". The questions below pin them down.
Clarifying Questions
You: What do the items look like, and how many do we expect?
Interviewer: Short strings, like URLs or usernames. Plan for about a million of them.
You: How wrong is the filter allowed to be?
Interviewer: It must never say "no" for an item we added. Saying "yes" for an item we did not add is fine about 1% of the time.
You: Who picks the size: the caller, or the filter?
Interviewer: The constructor takes the number of bits and the number of hash functions, so we can test it exactly. But give us a helper that picks good values from the item count and error rate.
You: Do we need to remove items?
Interviewer: No. Items are only ever added.
You: Does the filter ever leave the process? Saved to disk, or built on one machine and read on another?
Interviewer: Yes, eventually. So the same item must always map to the same bits, on any machine.
You: Is it useful for add to say whether the item was new?
Interviewer: Yes. The crawler wants "check and add" in one call: if the item was definitely new, crawl it.
You: Several threads at once?
Interviewer: One thread for now.
The second answer is the heart of the problem: errors are allowed in one direction only. The fifth one quietly rules out a common shortcut, which we will meet in the Implementation section.
💡 Interview tip: Say the guarantee out loud early: "no false negatives, a tunable rate of false positives." Every design choice later (which hash, how many bits, why there is no delete) comes back to keeping that guarantee.
Final Requirements
- The filter has
mbits, all off at the start, and useskpositions per item. add(item)turns on the item'skbits and returnsTrueif at least one of them was off (the item was definitely new).might_contain(item)returnsTrueif all of the item's bits are on andFalseotherwise. An added item always returnsTrue.- The positions of an item are the same on every run and every machine, from a fixed, documented hashing scheme.
bit_count()returns how many bits are on, so callers can see how full the filter is.- A helper picks
mandkfrom an expected item countnand a target false positive ratep.
Out of scope
- Removing items (we come back to it in Extensibility).
- Growing the filter after it is full.
- Several threads at once (also in Extensibility).
- Storing the items themselves or listing them.