← Course home

Lookup and priority · Lesson 26 of 52

Hash tables with chaining

Trace collision buckets and understand how load factor relates to expected chain length.

Full lessonAbout 10 min

You will learn to trace entries in collision buckets and relate load factor and resizing to expected lookup.

Recommended first: Arrays and Python sequence costs.

On this page 6 sections

The idea

With 4 buckets, keys 0 and 4 share a bucket. Lookup of 0 still returns 1.

Separate chaining stores a list of key-value pairs in each bucket. The hash of the key picks the bucket. Insert appends a pair, or replaces the value if the key is already in that list. Lookup scans the list and compares keys. With a 4-bucket table, hash(0) % 4 and hash(4) % 4 are both 0. Inserting 0 and then 4 places both pairs in one chain. A later lookup of 0 walks that chain and returns 1. The library ChainingHashTable does this, and len stays 2 because the keys differ.

A worked trace

StepStateWhat changed
Bucketindex = 0Key 0 joined bucket 0.
Bucketindex = 0Key 4 joined bucket 0.
Bucketindex = 0Lookup of 0 returns 1.
Bucketindex = 0Lookup of 4 returns 2.

Why it is correct

The hash is only a suggestion of where to look. Equality of keys is the real test. Two keys that share a bucket remain distinct as long as the chain stores both pairs. Lookup does not stop at the first pair. Insert of a new key appends, which preserves the pairs already there. Insert of an existing key overwrites that pair's value instead of appending a duplicate. That is why a lookup that scans from the front finds the current value.

A common mistake

A shared bucket is a collision, not a missing key.

What it costs

The load factor is the number of keys divided by the number of buckets. With n keys and b buckets the average chain is that load factor, n / b. One lookup is Θ(1 + n / b) comparisons when keys spread evenly, and Θ(n) when every key lands in one bucket. Resizing when the load factor grows keeps the average chain short, and the resize itself copies every pair once. Extra memory is Θ(n + b) for the pairs and the bucket lists. Expected Θ(1) is not a worst-case promise.

Keys 0 and 4 share a bucket because the bucket count is 4 and both hashes are multiples of 4. A third key 8 would append behind them. Lookup of 0 would then compare 0, skip 4, and skip 8 only if 0 were missing. The chain length is the cost you pay.

Run it

python
from bitsandbytes.hash_tables.chaining import ChainingHashTabletable = ChainingHashTable(bucket_count=4)table[0] = 1table[4] = 2print(table[0], table[4])# prints: 1 2 

Practice and next step

Open Hash tables with linear probing next. Linear probing stores that collision in the next open slot, and a tombstone keeps the walk going.

Implementation and tests Optional reference

Library code 1

  • bitsandbytes.hash_tables.chaining

Use these references to compare the explanation with repository code and tests. A test link means it imports a lesson symbol; it does not by itself claim complete behavior coverage.

Step-by-step trace

Keys 0 and 4 share a bucket

ChainingHashTable with 4 buckets stores (0, 1) and (4, 2) in the same chain, because hash(0) % 4 and hash(4) % 4 are both 0. Lookup of 0 walks that chain.

index
0

State

  1. 00comparing

Key 0 joined bucket 0.

Practice with feedback

Check your understanding

Try each checkpoint before revealing the feedback. Ask for a hint when you need one. Answers and hints stay in this browser when local storage is available.

1n keys sit in b buckets. What is the average chain length?
28 keys sit in 4 buckets. What is the average chain length?

Progress is never sent to an account or synced to another device. If local storage is blocked, choices last only until you leave this page.