Lesson 2: Finding Needles Without Scanning the Haystack — Hash Tables and Collisions

Learning objective

Students will compute array bucket indices using a deterministic hash function (h(k) mod M), build a 7-bucket hash table with separate chaining to resolve collisions, and compare the number of comparisons needed for an unsorted list scan vs. a hash table lookup.

Materials

Vocabulary

Lesson flow

1. Opening: Why Not Just Scan a List? (8 min)

  1. Suppose a school library database stores n = 70,000 book barcodes in an unsorted list.
  2. To check if barcode B-94821 is checked out, a linear scan checks item 1, item 2, item 3... taking 35,000 comparisons on average and 70,000 comparisons in the worst case (O(n)).
  3. Ask: "What if, instead of searching every shelf, we had a math formula that looked at the letters of a key and told us the exact shelf number in 1 step?"
  4. That is a Hash Table (called a dict in Python or Map in JavaScript).

2. Direct instruction: Computing a Toy Hash Function & Modulo Indexing (15 min)

  1. Define a simple, hand-computable hash rule for a table with M = 7 buckets numbered 0 through 6:
  2. Step A: Add the alphabet positions (A=1, B=2, ..., Z=26) of the letters in the key (sum).
  3. Step B: Divide sum by 7 and take the remainder: bucket = sum mod 7 (the remainder is always 0, 1, 2, 3, 4, 5, or 6!).
  4. Work through 6 animal keys together on the board:
Key Letter Values sum Division by 7 bucket = sum mod 7
"CAT" 3 + 1 + 20 24 24 = 3 × 7 + 3 Bucket 3
"DOG" 4 + 15 + 7 26 26 = 3 × 7 + 5 Bucket 5
"ANT" 1 + 14 + 20 35 35 = 5 × 7 + 0 Bucket 0
"OWL" 15 + 23 + 12 50 50 = 7 × 7 + 1 Bucket 1
"BEE" 2 + 5 + 5 12 12 = 1 × 7 + 5 Bucket 5 (COLLISION with "DOG"!)
"FOX" 6 + 15 + 24 45 45 = 6 × 7 + 3 Bucket 3 (COLLISION with "CAT"!)
  1. Show why Separate Chaining is essential:
  2. When "BEE" lands in Bucket 5, we must not erase "DOG". Instead, Bucket 5 holds a short chain: Bucket 5 → [ ("DOG", val), ("BEE", val) ].
  3. When looking up "BEE", the computer computes 12 mod 7 = 5, jumps straight to Bucket 5, checks "DOG" (not a match), and finds "BEE" on the 2nd comparison—still only 2 comparisons instead of scanning all 6 items!

3. Main activity: Pair Hash-Table Build & Worst-Case Adversarial Attack (17 min)

  1. Part A — Insert 3 More Keys (10 min): Student pairs draw the 7-bucket table (0..6) with the 6 animals above already placed, then compute and insert three more keys:
  2. "ELK" (5 + 12 + 11 = 28; 28 mod 7 = 0 → chains after "ANT" in Bucket 0)
  3. "HEN" (8 + 5 + 14 = 27; 27 mod 7 = 6 → lands in empty Bucket 6)
  4. "COD" (3 + 15 + 4 = 22; 22 mod 7 = 1 → chains after "OWL" in Bucket 1)
  5. Part B — Count Comparisons & Load Factor (7 min):
  6. Total items n = 9, total buckets M = 7 → Load factor α = 9 / 7 ≈ 1.29 items per bucket.
  7. What is the maximum number of key comparisons needed to find any of the 9 animals in our hash table? (Only 2 comparisons, because the longest chain has length 2!)
  8. Adversarial thought experiment: Why doesn't our hash function only check the first letter of a word? (Because in an English dictionary, thousands of words start with 'S', piling into one giant chain while 'X' is nearly empty!)

4. Discussion questions (5 min)

5. Closing check (5 min)

Using the same rule (letter sum mod 7, with A=1 ... Z=26):

  1. Compute the letter sum and bucket index (0..6) for the key "BAT" (B=2, A=1, T=20).
  2. Does "BAT" collide with any key in our 9-animal table from the lesson? If so, which bucket does it join, and which keys are already in that bucket?
  3. A hash table has M = 50 buckets and currently stores n = 35 keys. What is its load factor α, and why is a low load factor desirable?

Worked answers for closing check

  1. B(2) + A(1) + T(20) = 23. Dividing by 7 gives 23 = 3 × 7 + 2, so 23 mod 7 = 2 (Bucket 2).
  2. No collision! In our 9-animal table, Buckets 0, 1, 3, 5, 6 had items, while Bucket 2 and Bucket 4 were empty. So "BAT" is the first item in Bucket 2.
  3. Load factor α = 35 / 50 = 0.70. A low load factor means most buckets have 0 or 1 item, keeping lookups close to 1 comparison (O(1)).

Differentiation

Teacher notes