Skip to the workspace
Pebellslearn by building
Number Theory for Algorithms  ›  Capstone Project

Capstone — Quarry's Hash Kernel

40 minA polynomial hash, a primality check on the modulus, and the birthday bound, combined into one report

dsa / Capstone — Quarry's Hash Kernel

Explore each scene at your own pace.

01 · What you're building

What you're building

Capstone: Quarry's Hash Kernel

Definition
A hash kernel turns strings into numbers with a polynomial hash taken modulo a prime, then checks the result: how many strings collided, how many buckets were used, and how many collisions chance alone would produce.
In plain words
It is a small machine that gives every word a numbered locker, then counts how many words ended up sharing a locker. It also tells you whether that many shared lockers is normal bad luck or a sign you picked the wrong number of lockers.

In short: Every earlier lesson in this section built one piece of arithmetic. The hash kernel puts them to work together: it folds real strings into numbers, picks a modulus that won't cluster them, and tells you whether the collisions it's seeing are bad luck or a bad choice of modulus.

What you're building

hash_kernel_report(strings, base, mod) -> {
    "hashes": [...],
    "collisions": int,
    "buckets_used": int,
    "is_prime_mod": bool,
    "expected_collisions": float,
}
hash_kernel_report(["cat", "dog", "bird", "cat"], 131, 97)
# {"hashes": [3, 70, 51, 3], "collisions": 1, "buckets_used": 3,
#  "is_prime_mod": True, "expected_collisions": 0.0619}
  • hashes: poly_hash(s, base, mod) for every string, in order — lesson 2's running total, modded every step.
  • collisions: how many pairs of strings landed on the same hash. "cat" appears twice, so it collides with itself once — that's not the modulus's fault, it's the same string twice, and a real system would need to compare the actual strings, not just their hashes, before trusting a match.
  • buckets_used: how many distinct hash values came out — the same "does capacity resist clustering" question from lesson 1, now measured instead of assumed.
  • is_prime_mod: lesson 1's is_prime, checking whether mod itself is a sound choice.
  • expected_collisions: the birthday bound — lesson 3's combinatorics, turned into a sanity check: for n strings landing uniformly at random across mod buckets, about how many colliding pairs should you expect by chance alone?
Scene 1 of 7
Project context and further resources
You're building a piece ofQuarry — the hash kernel
This piece — hash_kernel_report(): Hashes real strings, checks the modulus is prime, and compares measured collisions against the birthday bound.
Scenario Quarry is about to switch its hash table's modulus and wants a report proving the new choice won't cluster real keys before it ships.
hash_kernel_report.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
hash_kernel_report(strings, base, mod) → dictHashes, collisions, buckets used, whether mod is prime, and the birthday-bound estimate.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.