Data structures

Hash Table & Collisions

Add keys to a small hash table and watch the hash function assign buckets. Collisions are resolved with chaining.

5 minLevel 2/3The interactive stage stays ad-free.
Keys7Load factor1.00Collisions2
0
empty
1
apple1
2
empty
3
banana3cherry3elderberry3
4
date4
5
grape5
6
fig6

This table uses hash = sum(char codes) mod 7 with chaining. Keys in the same bucket form a chain. A high load factor means more collisions and slower lookups.

What you are seeing

1

The hash function converts a key into a bucket index, for example by summing character codes and taking a modulus.

2

A good hash spreads keys evenly. A collision happens when two keys land in the same bucket.

3

With chaining, each bucket stores a small list. Lookup hashes the key, then scans only that list.

Why it matters

Hash tables power dictionaries, caches, database indexes, and language runtimes. Average O(1) lookup is what makes them so useful.

Complexity & details

Average: O(1) insert/lookup/delete. Worst case: O(n) when many keys collide.

Frequently asked questions

What is a load factor?+

Load factor is the number of stored keys divided by the number of buckets. High load factors increase collisions; real hash tables resize to keep it low.

Why do hash table attacks matter?+

If an attacker can force many keys into the same bucket, operations degrade toward O(n). Randomized hashing is a common defense.