LOGBOOK

HELP

Quiz Entry - updated: 2026.11.26

What are the two parts of a hash table, and how does it decide where an entry goes?

A hash function h that turns a key into an index in [0, N−1], and an array of N cells (the buckets). The entry (k, v) is stored at index h(k).

Keys 25, 13 and 47 pass through h(x) = x mod 10 and land in buckets 5, 3 and 7 of a ten-cell array

* The hash function turns the key straight into an array index, so no search is needed. *

A hash table is an implementation of the map ADT. Instead of searching for a key, it computes where the key must be:

  1. Apply the hash function: i = h(k), for example h(x) = x mod N.
  2. Go straight to cell i of the array and store, read or delete the entry there.

h(k) is called the hash value or hash code of k. Reaching cell i is one array access, so as long as every key lands in its own cell, get, put and remove all take $O(1)$, independent of n.

Why this is truly constant: computing h(k) is a fixed calculation, not a loop over the entries (like computing 1 + 2 + … + n with the formula n(n+1)/2 instead of adding), and reading a[i] takes the same time whether i is 5 or 500,000.

The German name Streuspeicher ("scatter storage") captures the idea: a good hash function scatters the keys as widely as possible over the array. Keys can be any objects (numbers, names, people, stock symbols), as long as the function turns them into a valid index.

The catch is that there are far more possible keys than cells. Two keys can land on the same index; this is a collision, and every real hash table needs a strategy for it.

Going further: many textbooks split h into a hash code (key → any integer, Java's hashCode()) and a compression function (integer → [0, N−1], such as mod N). That lets one class define its hash code once and still work with tables of any size.

Go deeper:

  • doc Hash table — hashing, collision resolution, load factor and resizing in one article.
  • video MIT 6.006 Lecture 4: Hashing — why hashing gets below the comparison-sorting lower bound, and chaining in detail.

From Quiz: ADS / Sets, Maps and Hash Tables | Updated: Nov 26, 2026