Quick revision
The short answer
A Bloom filter keeps m bits and k hash functions: put() sets the k bit positions of a key, and mightContain() returns false if any of them is 0 and true only if all are 1, so it can give false positives but never false negatives. We size it with m = ceil(-n ln p / (ln 2)^2) and k = round((m / n) ln 2), which for 10 million keys at 1 percent gives about 96 million bits (12 MB) and 7 hash functions, derive the k positions by double hashing from one 128-bit Murmur3 hash, and in production prefer a tested implementation such as Guava's BloomFilter.
What to remember
In this answer, we implemented a Bloom filter: a bit array where put() sets k positions and mightContain() returns false if any position is 0, giving possible false positives but never false negatives. We sized it with m = ceil(-n ln p / (ln 2)^2) and k = round((m / n) ln 2), about 96 million bits and 7 hashes for 10 million keys at 1 percent, derived the k positions from one 128-bit Murmur3 hash by double hashing, and noted its limits: no deletion without a counting or Cuckoo filter, rising error beyond n, and no thread safety in a plain BitSet, which is why production code usually uses Guava's BloomFilter.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A Bloom filter keeps m bits and k hash functions: put() sets the k bit positions of a key, and mightContain() returns false if any of them is 0 and true only if all are 1, so it can give false positives but never false negatives. We size it with m = ceil(-n ln p / (ln 2)^2) and k = round((m / n) ln 2), which for 10 million keys at 1 percent gives about 96 million bits (12 MB) and 7 hash functions, derive the k positions by double hashing from one 128-bit Murmur3 hash, and in production prefer a tested implementation such as Guava's BloomFilter.
Why this question is asked
Bloom filters are everywhere: databases such as Cassandra and RocksDB use them to skip files that cannot contain a key, and caches use them to avoid lookups for keys never stored. Interviewers like asking for one because it is short to write, yet it tests hashing, bit manipulation and a little probability. In this answer, we will see how a Bloom filter answers its question, derive its size, implement it in Java with double hashing, try it, and look at its limits and the production alternative.
1How a Bloom Filter Answers "Have I Seen This?"
A Bloom filter is an array of m bits, all 0 at the start, plus k hash functions that each map a key to a position in the array. To add a key, we compute its k positions and set those bits to 1. To ask about a key, we compute the same k positions and look at the bits. If any of them is 0, the key was definitely never added, because adding it would have set that bit. If all of them are 1, the key was probably added, but those bits might also have been set by other keys. That is a false positive.
So the filter never says no to a key it contains, and occasionally says yes to a key it does not. In exchange, it needs only about 10 bits per key at a 1 percent error rate, however long the keys are.
2Choosing the Number of Bits and Hash Functions
Two formulas give the size for n expected items and a target false-positive rate p. The number of bits is m = ceil(-n x ln(p) / (ln 2)^2), and the best number of hash functions is k = round((m / n) x ln 2). The reasoning behind k is a balance: more hash functions check more bits per lookup, which makes a false positive less likely, but each key also sets more bits, which fills the array faster. The formula picks the value where the array ends up about half full, which minimises the false-positive rate.
| Target p | Bits m | Memory | Hash functions k |
|---|---|---|---|
| 10 percent | about 47.9 million | about 6 MB | 3 |
| 1 percent | 95,850,584 | about 12 MB | 7 |
| 0.1 percent | about 143.8 million | about 18 MB | 10 |
For n = 10 million and p = 1 percent, ln(0.01) is about -4.605 and (ln 2)^2 is about 0.4805, so m = 10,000,000 x 4.605 / 0.4805, about 95.85 million bits, which is roughly 12 MB. Then m / n is about 9.585, and 9.585 x 0.693 is about 6.64, which rounds to k = 7.
3Getting k Positions From Two Hashes
Computing seven independent hash functions per key would be slow. Kirsch and Mitzenmacher showed that we can derive all k positions from two hash values, h1 and h2, as position_i = (h1 + i x h2) mod m for i from 0 to k - 1, without hurting the false-positive rate in practice. This is called double hashing. A 128-bit Murmur3 hash gives us both values in one computation: the first 64 bits are h1 and the second 64 bits are h2. We take Murmur3 from Guava.
4The Implementation
Here is the whole filter. It keeps the bits in a java.util.BitSet, which is fine up to about 2 billion bits; a much larger filter would use a long[] indexed by long positions.
Notice Math.floorMod: h1 + i x h2 can overflow into a negative long, and the ordinary % operator would then give a negative position. floorMod always returns a value between 0 and m - 1.
5Trying It Out
A quick run shows the behaviour, including the false-positive rate we sized for.
If we keep adding beyond n, the array fills up and the false-positive rate rises quickly: at 20 million keys in the same filter it is already around 15 percent. A filter must be sized for the largest n it will see.
6Limits and the Production Choice
A Bloom filter cannot delete keys. Clearing a key's bits might clear bits that other keys also set, which would create false negatives, the one thing a Bloom filter must never produce. When deletion is needed, a counting Bloom filter, which keeps a small counter instead of a bit, or a Cuckoo filter, which stores short fingerprints, supports it at extra memory cost.
Our BitSet is also not thread-safe, so concurrent writers would need a lock or a filter built on an AtomicLongArray. In production, a tested implementation is the better choice: Guava's BloomFilter.create(Funnels.stringFunnel(StandardCharsets.UTF_8), 10_000_000, 0.01) does the sizing, the double hashing and the bit storage for us, and recent Guava versions document it as safe for concurrent use (check your version).
7Follow-up questions
How would you merge two filters built with the same parameters?
Take the bitwise OR of their bit arrays. A key added to either filter has all its k bits set in that filter, so all of them are set in the union, and mightContain() still never gives a false negative. This only works if both filters have the same m, the same k and the same hash functions and seeds, because the positions must mean the same thing. In our class that is bits.or(other.bits), and Guava offers putAll(other) after checking compatibility. The merged filter's false-positive rate corresponds to the combined number of distinct keys, so two filters each filled to n produce one that behaves like a filter holding up to 2n keys.
8Conclusion
In this answer, we implemented a Bloom filter: a bit array where put() sets k positions and mightContain() returns false if any position is 0, giving possible false positives but never false negatives. We sized it with m = ceil(-n ln p / (ln 2)^2) and k = round((m / n) ln 2), about 96 million bits and 7 hashes for 10 million keys at 1 percent, derived the k positions from one 128-bit Murmur3 hash by double hashing, and noted its limits: no deletion without a counting or Cuckoo filter, rising error beyond n, and no thread safety in a plain BitSet, which is why production code usually uses Guava's BloomFilter.
Open with
A Bloom filter is a bit array of size m with k hash functions. To add a key I set the k bits it hashes to; to check a key I look at those k bits, and if any is zero it's definitely not there, if all are one it's probably there.
Then, if they want more
So false positives are possible, false negatives never. For n expected items and false-positive rate p, the bits are m equals ceiling of minus n times ln p over ln two squared, and k is m over n times ln two, rounded.
For ten million keys at one percent that's about ninety-six million bits, roughly twelve megabytes, and seven hash functions.
In Java I'd store the bits in a BitSet, or a long array for very large m, and instead of seven separate hash functions I'd use double hashing: take a 128-bit Murmur3 hash, split it into h1 and h2, and use h1 plus i times h2 modulo m, with floorMod to avoid negative indexes.
Put sets all k positions, mightContain returns false on the first zero. Keys can't be removed, because clearing a bit could remove other keys, so for deletion I'd use a counting Bloom filter or a Cuckoo filter, and the false-positive rate rises once you go beyond n.
In production I'd use Guava's BloomFilter.create with a string funnel, n and p, and note that a plain BitSet isn't thread-safe.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Implement a Bloom filter in Java for String keys, sized for n expected items and a...": https://scrivio-interview-library.netlify.app/architecture/distributed-systems/1773a6ce58/