Hashing · bucket size

Bucket Size Lab

Both tables below have room for exactly 30 keys. The left one has 30 buckets that each hold 1 key. The right one has fewer, bigger buckets, such as 10 buckets of 3. The same keys go into both, using h(key) = key mod (number of buckets).

CCollision

The key's bucket already holds at least one other key. Fewer buckets means more keys share a bucket, so collisions go up as buckets get bigger.

!Overflow

The key's bucket is already full, so the key does not fit and must be stored somewhere else. With bucket size 1, every collision is an overflow.

bBucket size

How many keys one bucket holds. A bucket of 3 can absorb two collisions before it overflows, so overflows go down as buckets get bigger.

Fill the two tables

12

The probabilities behind it

These curves assume each key is equally likely to land in any bucket. The x-axis is the number of keys already in the table, out of 30 spaces, so 15 keys is a load factor of 0.5. The dashed line marks the slider. Hover over a chart to read exact values, or click to move the slider.

Chance the next key collides

Its bucket already has a key in it.

Chance the next key overflows

Its bucket is already full.

Chance of at least one overflow

Somewhere among all the keys inserted so far.

Bucket sizeBucketsNext key collidesNext key overflowsAt least one overflow so farExpected collisions so farExpected overflows so farSimulated: at least one overflowSimulated: avg overflows
Checks the formulas by filling 2,000 tables with random keys at the current number of keys.