Practice this topic in a realistic system design interview
Comparing two small sets is easy. Comparing billions of large sets with each other is not. MinHash gives each set a small, fixed-size summary that lets us estimate how similar two sets are without comparing them item by item.
In this chapter, we'll learn what MinHash is, how it estimates the similarity between two large sets using small signatures, and how it combines with a technique called locality-sensitive hashing to find similar items among billions without comparing every pair.
Suppose you're building a search engine that crawls billions of web pages. Many of these pages contain almost identical content:
You don't want all these near-duplicate pages showing up in search results, so you need a way to identify pages that are very similar.
Finding exact duplicates is straightforward. You can hash each page, and if two pages have the same hash, you can treat them as identical.
But regular hashes don't work well for near-duplicates. Even changing a single character can produce a completely different hash. So two pages could be 95% identical, and their hashes would give you no indication of that similarity.
What we need is a way to measure how similar two documents are, without having to compare every pair of pages directly. And it needs to work efficiently across billions of pages.
To measure how similar two documents are, we first need a way to define similarity. One common measure is called Jaccard similarity, which compares two sets of items.
The idea is simple. We count how many items the two sets have in common, then divide that by the total number of unique items across both sets:
For example:
cat, dog, fish, and bird.cat, dog, fish, and lion.Three items appear in both sets: cat, dog, and fish. And there are five unique items in total: cat, dog, fish, bird, and lion. So the Jaccard similarity is 3 divided by 5, which gives us 0.6.
If two sets are identical, their similarity is 1. And if they have no items in common, their similarity is 0.
But documents aren't sets. They're sequences of words. So to use Jaccard similarity, we first need to convert each document into a set of shingles.
A shingle is simply a short sequence of consecutive words. For example, if we use three-word shingles, the sentence "the quick brown fox jumps" gives us three shingles:
This approach helps preserve some of the original word order. Documents that share many of the same phrases will also share many shingles.
And if someone changes just one word in a long article, only the shingles around that word are affected. The rest remain unchanged, so the Jaccard similarity between the original and modified documents stays high.
Now that we have a set of shingles for each document, we can compare two documents using Jaccard similarity. But there are two challenges.
So we need to solve two problems. We need a way to make individual comparisons much cheaper, and we need to avoid comparing most pairs in the first place.
MinHash helps us solve the first problem, while locality-sensitive hashing, which we'll cover later, helps us solve the second.