Advanced topics · Exercise 98 of 100

Bloom filter

Start with all bits at zero and use the same hash functions to turn an item into bit positions each time. Inserting an item sets all its bits to one. As long as bits are never cleared, a zero at any checked position rules the item out, while all ones can also come from other items.

Try it yourself

Inserting pear sets bits 1 and 5, and inserting plum sets bits 3 and 5. Only those two words have been inserted. A lookup for fig checks bits 1 and 3 and returns maybe if both are set or absent otherwise. What word does it return?

One-step hint

Make a single row of bits for both insertions. Mark all positions they set, then inspect the two positions requested by the fig lookup together.

Worked solution
  1. Pear supplies the one at position 1 and plum supplies the one at position 3, so both positions checked for fig contain ones.
  2. Those shared positions satisfy the lookup rule even though fig was never inserted, so the filter returns maybe.
DSA compiler