Load factor and resizing
The load factor is how full a hash table is. When it passes a threshold the table doubles and every key moves to a new bucket.
Before you start: Open addressing: linear, quadratic probing, double hashing.
The idea
Four buckets now hold 11 keys. Finding 13 means walking three keys in chain 1.
The table never grew while keys kept arriving. Chains get longer as n grows, so lookups drift from O(1) toward O(n).
Intuition
The load factor is keys ÷ buckets. Here 6 keys sit in 8 buckets, so it is 0.75, a common limit.
The load factor is the average chain length. Keeping it under a constant keeps every chain short on average.
Past the limit, the table doubles from 4 to 8 buckets. Each key moves to key mod 8, so 5 and 14 change bucket.
The bucket number depends on the table size. A new size gives each key a new bucket, so every key must be hashed again.
Complexity
Most inserts touch one key. A resizing insert also moves every key: 1 + 4, then 1 + 7, then 1 + 13.
A resize costs O(n) because every key is rehashed. Doubling makes those spikes rare: the next one waits until n has doubled.
Doubling keeps the total work for n inserts under 3n. That is O(1) amortized per insert.
The moves add up to n + n/2 + n/4 + … < 2n. Growing by a fixed 4 buckets instead resizes every few inserts, and the total grows like n².
Pitfalls
Copying chain i into new bucket i is a bug. 5 stays in bucket 1, but get(5) now looks in bucket 5 and misses.
Every key must be placed by key mod newM. A copy by index keeps the old bucket numbers, which are only right for the old size.
Shrinking below 0.5 right after growing undoes the resize. Insert, delete, insert then resizes every time.
After doubling, the load factor is about 0.4. A shrink limit of 0.25 leaves a gap, so alternating inserts and deletes cause no resizes.
Variations
Open addressing stores keys in the slots themselves. It usually grows at 0.5 so probe runs stay short.
Some systems move a few keys per operation instead of all at once. Redis does this, so no single insert waits for O(n).
Both tables live side by side during the move. Lookups check the old table, then the new one, until every key has moved.
Code
Pseudocode
put(key)
i ← key mod m
append key to bucket[i]; n ← n + 1
if n ÷ m > 0.75: resize(2m)
resize(newM)
old ← buckets; buckets ← newM empty chains; m ← newM
for each chain in old
for each key in chain: append key to bucket[key mod newM]
Python
def put(table, key):
i = key % len(table.buckets)
table.buckets[i].append(key)
table.n += 1
if table.n / len(table.buckets) > 0.75:
resize(table, 2 * len(table.buckets))
def resize(table, new_m):
old = table.buckets
table.buckets = [[] for _ in range(new_m)]
for chain in old:
for key in chain:
table.buckets[key % new_m].append(key)
JavaScript
function put(table, key) {
const i = key % table.buckets.length;
table.buckets[i].push(key);
table.n++;
if (table.n / table.buckets.length > 0.75) resize(table, 2 * table.buckets.length);
}
function resize(table, newM) {
const old = table.buckets;
table.buckets = Array.from({ length: newM }, () => []);
for (const chain of old) {
for (const key of chain) table.buckets[key % newM].push(key);
}
}
Java
void put(int key) {
int i = key % buckets.length;
buckets[i].add(key);
n++;
if (n > 0.75 * buckets.length) resize(2 * buckets.length);
}
void resize(int newM) {
List<Integer>[] old = buckets;
buckets = newBuckets(newM);
for (List<Integer> chain : old) {
for (int key : chain) buckets[key % newM].add(key);
}
}
Practice problems (6)
New positions after resize Warm-up
A chaining table starts with 4 buckets and uses key mod m, where m is the current number of buckets. It doubles when keys ÷ buckets goes above 0.75. Insert 5, 9, 2, 14. The fourth insert triggers a resize to 8 buckets. Give the new bucket of each key.
Choose the threshold Warm-up
You write a hash table with separate chaining. It doubles when the load factor (keys ÷ buckets) passes a limit. Searches should stay short, and the table should not waste most of its buckets. Which limit do you pick?
Amortized cost Standard
A hash table starts with 4 buckets and doubles whenever the load factor passes 0.75. A resize rehashes every key, so one resize of n keys costs O(n). You insert n keys. What is the cost per insert, averaged over all n inserts?
Count the resizes Standard
A hash table starts with size buckets. You insert the distinct keys of keys one by one. After each insert, if keys ÷ buckets is above 0.75, the number of buckets doubles.
Return how many times the table doubles.
Total rehash moves Standard
A hash table starts with size buckets. You insert the distinct keys of keys one by one. After each insert, if keys ÷ buckets is above 0.75, the table doubles and rehashes every key it holds. Each rehashed key counts as one move.
Return the total number of moves over all inserts. Compare it with the number of keys: it stays below 2n.
Implement resize Challenge
Build a chaining hash table that grows. It starts with size buckets, each a list, and a key goes to bucket key mod m, where m is the current number of buckets. Insert the distinct keys of keys in order; each new key joins the end of its chain.
After each insert, if keys ÷ buckets is above 0.75, resize: make 2m empty buckets, then walk the old buckets from 0 to m − 1, each chain front to back, and append every key to bucket key mod 2m.
Return every key of the final table, read bucket by bucket from bucket 0, each chain front to back. Do not use a built-in map or set.
Related topics
Next on the path: Hash sets and maps in practice: counting, lookup, deduplication