Module 11 of 39

Hash tables: collisions, load factor, and lookup

Find a value by key, handle collisions, and understand what expected constant-time lookup assumes.

A contacts app may need to find a phone number from a name. Searching every entry works, but repeated searches can become costly. A direct-address table would use the key itself as an array position. That works when keys come from a small, known range. Names do not. A hash table turns a key into a position using a hash function.

Background for this page. A hash function maps a key to a number used to choose a table position. A collision means different keys choose the same position; comparing actual keys remains necessary. Related explanations: Arrays, linked lists, stacks, and queues · Counting, probability, and expectation for algorithms.

The function does not have to give every key a different position. In fact, when there are more possible keys than positions, two keys must sometimes land together. That is a collision. A correct table must handle collisions; ignoring them would lose data.

One approach is chaining: each table position holds a small collection of entries. To look up a key, hash it, then check entries at that position for the exact key. Another is open addressing: keep entries in the table itself and probe other positions according to a fixed rule when the first is occupied. In either case, a hash match alone is not proof that two keys are equal. The stored key must be checked.

# Hashing chooses a bucket; equality finds the key.
bucket = table[hash(key) mod table_size]
# Collisions can leave several entries here.
for each entry in bucket:
    if entry.key equals key: return entry.value
return not found

Ada and Mira both map to bucket 2, which keeps both entries for a later exact-key check

The bucket narrows the search. It does not replace the equality check between stored keys.

Hash tables often provide expected O(1) lookup, insertion, and deletion. That claim depends on keys being spread well and on the table not becoming too full. A poor hash function, adversarial keys, or a high load factor can make operations slow; a worst-case lookup may still inspect O(n) entries. Growing the table and rehashing entries costs time, but it can keep ordinary operations fast across a long sequence.

A hash function should be consistent: equal keys must produce equal hashes while they are in the same table. It should also spread the expected keys across available positions. The table size, collision method, deletion rule, and expected key distribution all matter. An open-addressed table cannot usually delete an item by simply clearing its slot, because later searches may need to probe past that slot.

Hash tables give quick lookup by exact key, but they do not naturally give keys in sorted order or answer “what is the next larger key?” A search tree is a better candidate for those questions. Choose the structure by the operations you need, not by one attractive complexity label.

Why deletion needs care

In open addressing, a colliding key may be stored later in a probe sequence. If deleting an earlier key leaves a never-used empty slot, lookup may stop there and incorrectly declare the later key absent. A deletion marker, or tombstone, tells lookup to continue. Chaining instead removes a record from its bucket. Both designs must distinguish updating an existing key from adding a distinct colliding key. Resizing usually recomputes positions because the position formula depends on capacity.

Practice. A table has five buckets. Keys "Ada" and "Mira" both hash to bucket 2. What must a chaining lookup for "Mira" do? Why is checking only the bucket number insufficient?

Answer. It visits bucket 2 and compares its entries’ actual keys until it finds "Mira" or reaches the end. The bucket number identifies a place to search, not a unique key.

Try another case. Why is copying every bucket to the same index insufficient after resizing?

Reasoning. The changed capacity can change each key’s correct position. Values can be reused, but table locations need rebuilding.

View the full course and choose another topic.

Image preview

Fit