Hashing · Exercise 69 of 100
Basic Bloom filter
A Bloom filter starts with all bits at zero and uses several hash functions to pick which bits to set to one for each item. A lookup checks the same positions, so finding a zero rules the item out as long as no bits have been cleared. Finding only ones means the item might be present because other items can set those bits too.
Try it yourself
In an empty Bloom filter, inserting mint sets bits 1 and 4, and inserting sage sets bits 2 and 5. A lookup for dill checks bits 1 and 5, while a lookup for basil checks bits 2 and 3. Which of these two uninserted herbs passes the lookup?
One-step hint
Shade all positions set by mint or sage. For each lookup, place a finger on each of its two positions and check whether both are shaded.
Worked solution
- After the two insertions, bits 1, 2, 4, and 5 are one. Bit 3 is still zero, so basil fails its check.
- Mint set bit 1 and sage set bit 5. Both bits checked for dill are one, so dill passes.