3.3.1 · D3Hashing

Worked examples — Hash function — properties - deterministic, uniform, fast

2,637 words12 min readBack to topic

Before anything: recall the two workhorses we will use over and over.


The scenario matrix

Every worked example below is tagged with the cell of this matrix it covers. Together they fill the whole grid.

Cell Case class What could go wrong
A Positive key, prime Baseline — should spread well
B Key (zero input) Does hash sensibly?
C Key vs key What if the key is smaller than the table?
D Negative key can go negative in real code
E Bad modulus (power of 10/2) Clustering — low-digit blindness
F Degenerate table Everything collides — limiting case
G String key (order matters) Anagrams must differ
H Huge key / overflow Keep numbers in range mid-computation
I Real-world word problem Phone numbers / usernames
J Exam twist Load factor + rehash decision

Worked examples

Example 1 — Cell A: the clean baseline

Figure — Hash function — properties -  deterministic, uniform, fast

Example 2 — Cell B & C: zero, and a key smaller than the table


Example 3 — Cell D: negative keys (the real-code trap)


Example 4 — Cell E & F: the bad modulus and the degenerate table


Example 5 — Cell G: strings where order matters


Example 6 — Cell H: huge key without overflow


Example 7 — Cell I: real-world word problem


Example 8 — Cell J: the exam twist (load factor → rehash decision)


Case-coverage recap

Recall Which example covered which cell?

A (positive, prime ) ::: Example 1 B (zero key) ::: Example 2 C (key smaller than table) ::: Example 2 D (negative key + language sign trap) ::: Example 3 E (power-of-10 modulus clustering) ::: Example 4 Part 1 F (degenerate , all collide) ::: Example 4 Part 2 G (anagram strings, order matters) ::: Example 5 H (huge key, overflow-safe mod) ::: Example 6 I (real-world username word problem) ::: Example 7 J (exam twist: load factor + rehash) ::: Example 8


Connections