Data Structures · Visualization

Bloom Filter: Introduction

Bloom Filter

A Bloom filter is a compact, probabilistic structure that answers one question extremely fast and with very little memory: 'have I possibly seen this item before?'. Time: O(k) for add/check, where k is the number of hash functions (constant, independent of item count). Space: O(m), the size of the bit array (much smaller than storing actual items).

Big-O: Space-efficient, no false negatives; possible false positives.

Bloom Filter: Introduction

Bloom Filter

A Bloom filter is a compact, probabilistic structure that answers one question extremely fast and with very little memory: 'have I possibly seen this item before?'. It never gives a false negative (if it says 'no', the item is definitely not there), but it can occasionally give a false positive (saying 'maybe yes' for something never actually added). It works using a bit array and several hash functions that each flip specific bits on when an item is added.

How Bloom Filter: Introduction Works

  1. Create a bit array of a chosen size, with every bit initially set to 0.
  2. Choose several different hash functions.
  3. To add an item, run it through each hash function to get several positions, and set the bits at those positions to 1.
  4. To check an item, run it through the same hash functions; if every resulting bit position is 1, the item is 'probably present'; if any bit is 0, it is definitely absent.

Bloom Filter: Introduction Worked Example

Input: A Bloom filter with 3 hash functions and a 10-bit array

  1. Add 'apple': hash functions give positions 1, 4, 7 → set bits 1, 4, 7 to 1
  2. Add 'banana': hash functions give positions 2, 4, 9 → set bits 2, 4, 9 to 1
  3. Check 'apple': positions 1, 4, 7 are all 1 → probably present (correct)
  4. Check 'cherry': hash functions give positions 1, 2, 5 → bit 5 is 0 → definitely absent

Output: 'apple' -> probably present, 'cherry' -> definitely absent

📖 Introduction: this is an overview of the topic, so there's nothing to run here. Pick an operation from the side menu to see a live, step-by-step animation.
Bloom Filter: Introduction
A compact, probabilistic set: it can tell you an item is "definitely not present" or "probably present", using far less memory than storing every item.
0
0
0
1
0
2
0
3
0
4
0
5
0
6
0
7
0
8
0
9
0
10
0
11
0
12
0
13
0
14
0
15
Active hash bit Bit set to 1 Bit = 0
Bloom Filter: Introduction
Press ▶ Run to watch it step by step.
Python · Bloom Filters in Code
# A Bloom filter: a bit array + k hashes.
m, k = 1000, 3
bits = [0] * m

# add -> set k bits; contains -> check all k.
# example: intro

Bloom Filter: Introduction Time & Space Complexity

TimeO(k) for add/check, where k is the number of hash functions (constant, independent of item count)
SpaceO(m), the size of the bit array (much smaller than storing actual items)

When to Use Bloom Filter: Introduction

  • Quickly checking if a username or URL has already been seen, before doing an expensive database lookup
  • Web browsers checking if a URL might be malicious (Google Safe Browsing)
  • Databases (e.g. Cassandra, BigTable) filtering out unnecessary disk lookups
  • Network routers filtering out unwanted packets

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.