Back to Discover
Curiosity

Inside the Hash Function

How keys are converted into numeric values, how those values are squeezed into a fixed array range with modulo arithmetic, and why collisions are an expected consequence.

Before you enter

A complete interactive classroom, not just a preview.

Start when you are ready to enter this Stage's 9 scenes and explore, respond, and learn as you go.

9
Scenes
18 min
Estimated
Content language: en-US
Start this Stage
Sign-in may be required to play
What happens inside
  1. 01From key to slot in one step?slide
    Question

    Pose the driving question: how can a dictionary jump straight from a word to its array slot, and what arithmetic is actually being performed on the key?

    • A hash table claim: lookup in roughly O(1) time
    • The mystery: keys are strings or objects, not integers
    • Some function must compress keys into slot numbers
  2. 02Predict the mappinginteractive
    Prediction

    Learner enters a few short keys, guesses the slot index each would land in for a table of size 7, and commits to a rule before the math is shown.

    • Choose keys like 'cat', 'dog', 'ant'
    • Predict each slot index in a size-7 table
    • Articulate the rule you are assuming
  3. 03Characters have codesslide
    Evidence

    Show that every character maps to a small integer (e.g., ASCII 'a'=97, 'b'=98), so any string can be treated as a sequence of numbers.

    • Letters are already numbers under the hood
    • A string is a list of integers
    • That list is the raw material for hashing
  4. 04Fold the bytes into one numberinteractive
    Evidence

    A live calculator where the learner enters a key, picks a folding scheme (sum of codes vs. base-101 fold), and watches the intermediate integer appear.

    • Sum-of-codes version: 97+99+116 = 312 for 'cat'
    • Base-fold version multiplies by a prime at each step
    • Both collapse a string into a single integer
  5. 05Modulo squeezes the number into a slotslide
    Evidence

    Show that taking the folded value modulo the table size (e.g., 312 mod 7 = 4) yields an index that is always in [0, size − 1], no matter how large the intermediate number is.

    • Modulo N gives a result from 0 to N−1
    • This is the actual slot index
    • A larger table would just change N
  6. 06The three-step recipeslide
    Explanation

    Put the pieces together: encode the key as numbers, fold them into one integer, reduce it modulo the array size. This is the full hash function.

    • Step 1: convert each character to its code
    • Step 2: combine into a single integer
    • Step 3: take the result mod table_size
  7. 07When two keys land on the same slotslide
    Boundary

    Demonstrate that 'cat' and 'act' can fold to the same integer, producing the same slot — a collision is a mathematical inevitability, not a bug.

    • Pigeonhole: more keys than slots means some must collide
    • Anagrams are an obvious collision family
    • Collisions are handled separately, not by the hash itself
  8. 08Hash a new key yourselfinteractive
    Transfer

    Learner hashes a fresh key with a table size of their choice, step by step, and checks the slot index. Then they change the table size and predict how every slot index shifts.

    • Apply the three-step recipe unaided
    • Change table size and recompute
    • Notice that the same key can land in a different slot
  9. 09Answering the driving questionslide
    Resolution

    State the answer directly: a hash function encodes the key as numbers, folds them into one integer, and takes the remainder modulo the table size to produce a slot index.

    • Keys become integers through character codes
    • Folding combines those integers into one
    • Modulo maps that integer into a valid slot
    • Collisions are expected, not errors
Discussion

Discussion threads for a Stage aren't available yet.

Where this leads

This path ends here.

Explore more

More in Technology & Computing

See all