A table of people keyed by a 10-digit ID number such as 025-612-0001 uses h(x) = last four digits and N = 10,000 cells. Why the last four digits and not the first four?
A hash function should spread keys evenly. The last digits of an ID vary almost randomly, while leading digits often encode a region or year, so many IDs would share the same first four digits and collide.
With h = last four digits, 025-612-0001 goes to cell 1, 981-101-0002 to cell 2, 451-229-0004 to cell 4 and 200-751-9998 to cell 9998. Each key costs one array access.
The choice of digits is the whole quality question. If the leading digits encode where or when a number was issued (US social security numbers historically started with an area code), thousands of people from one region share them, and they would pile into a few cells while most of the 10,000 stay empty. The trailing digits are close to a counter, so they spread evenly.
The general rule: a good hash function
- uses the parts of the key that vary most,
- spreads typical keys uniformly across [0, N−1],
- is cheap to compute, because it runs on every operation.
This one still collides as soon as two IDs end in the same four digits, as 981-101-0004 and 451-229-0004 do.