100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What is a Bloom Filter?

Learn what a Bloom filter is, how false positives work, why deletion is unsafe, and how to explain it in a technical interview.

hardQ80 of 227 in Data Structures & Algorithms Est. time: 6 minsLast updated:
Open Code Lab

Expected Interview Answer

A Bloom filter is a space-efficient probabilistic data structure that tests whether an element is possibly in a set or definitely not in a set, using a bit array and several hash functions, trading a tunable false-positive rate for massive memory savings and never producing false negatives.

To add an element, the filter runs it through k independent hash functions, each producing an index into an m-bit array, and sets all k bits to 1. To check membership, the same k hash functions are computed and all k corresponding bits are checked: if any bit is 0, the element is definitely not in the set; if all bits are 1, the element is probably in the set, but could be a false positive caused by other elements' bits overlapping. There is no way to remove an element from a standard Bloom filter, since clearing a bit could break membership tests for other elements sharing that bit. The false-positive rate is tunable by choosing m and k relative to the expected number of elements n, and Bloom filters are used to avoid expensive lookups, like checking a disk-backed database or a network cache, before doing the real, costly check.

  • Extremely memory-efficient compared to storing actual elements
  • O(k) constant-time insert and lookup
  • Never gives false negatives
  • Tunable false-positive rate via array size and hash count

AI Mentor Explanation

A Bloom filter is like a scorer waving several colored flags on a wall grid every time a player's name is entered into a tournament, one flag color per hash of the name, instead of writing the full name down. To check if a player has registered, the scorer looks for all of that player's flag colors on the grid; if even one is missing, the player definitely never registered, but if all are present, they probably registered โ€” though another combination of other players' flags could have coincidentally lit up the same colors. There is no way to safely take a player's flags down once raised, since other players might share some of those same flag positions. This trade-off, occasional false alarms but zero missed registrations, in exchange for a much smaller wall, is exactly what a Bloom filter buys.

Step-by-Step Explanation

  1. Step 1

    Initialize an m-bit array

    Start with all m bits set to 0, and pick k independent hash functions.

  2. Step 2

    Insert by setting k bits

    Hash the element with all k functions and set each resulting bit position to 1.

  3. Step 3

    Query by checking k bits

    Hash the element the same way; if any of the k bits is 0, the element is definitely absent.

  4. Step 4

    Accept the false-positive trade-off

    If all k bits are 1, the element is probably present; tune m and k against expected n to control the false-positive rate.

What Interviewer Expects

  • Explain the bit array + k hash functions mechanism clearly
  • State it never produces false negatives, only possible false positives
  • Explain why standard Bloom filters do not support deletion
  • Give a real use case: cache/database lookup avoidance, spell checkers, network routers

Common Mistakes

  • Claiming a Bloom filter can give a definitive "yes, it exists" answer
  • Forgetting deletion is unsafe without a counting Bloom filter variant
  • Confusing it with a regular hash set that stores actual elements
  • Not knowing that more hash functions or a bigger array lowers the false-positive rate

Best Answer (HR Friendly)

โ€œA Bloom filter is a compact structure that can tell you for certain something is NOT in a set, but only probably IS in a set, using very little memory. I would reach for one when I need a fast first-pass filter before an expensive lookup, like checking a cache before hitting a database, since a small chance of a false positive is fine as long as I never miss something that's actually there.โ€

Code Example

Simple Bloom filter
import hashlib

class BloomFilter:
    def __init__(self, size=1000, num_hashes=3):
        self.size = size
        self.num_hashes = num_hashes
        self.bits = [0] * size

    def _hashes(self, item):
        for i in range(self.num_hashes):
            digest = hashlib.sha256(f"{i}:{item}".encode()).hexdigest()
            yield int(digest, 16) % self.size

    def add(self, item):
        for idx in self._hashes(item):
            self.bits[idx] = 1

    def might_contain(self, item):
        return all(self.bits[idx] == 1 for idx in self._hashes(item))

bf = BloomFilter()
bf.add("user_42")
print(bf.might_contain("user_42"))   # True
print(bf.might_contain("user_99"))   # False (or rare false positive)

Follow-up Questions

  • How would you calculate the optimal number of hash functions for a given false-positive rate?
  • What is a counting Bloom filter and how does it support deletion?
  • How would you resize a Bloom filter as the element count grows?
  • Where have you seen Bloom filters used in real production systems?

MCQ Practice

1. If a Bloom filter says an element is NOT in the set, what can you conclude?

Bloom filters never produce false negatives โ€” a "not present" answer is always correct.

2. Why can a standard Bloom filter not support deletion?

Bits are shared across elements, so clearing one on deletion could make another element wrongly appear absent.

3. What two parameters primarily control a Bloom filter's false-positive rate?

The false-positive rate is a function of the bit array size m, the number of hash functions k, and the number of inserted elements n.

Flash Cards

What two possible answers can a Bloom filter membership query give? โ€” "Definitely not present" or "probably present" (possible false positive).

Can a standard Bloom filter produce a false negative? โ€” No โ€” false negatives never happen, only false positives are possible.

Why is deletion unsafe in a basic Bloom filter? โ€” Bits are shared across multiple elements, so clearing one can break membership tests for others.

Name a real-world use case for a Bloom filter. โ€” Avoiding expensive disk/database lookups by first checking a Bloom filter cache (e.g. in databases like Cassandra or web crawlers).

1 / 4

Continue Learning