Hash tables
A hash table uses a hash function to decide where to store an item. In this exercise, a simple modulus operation maps an ID to one of several buckets.
Interactive bucket simulator
Section titled “Interactive bucket simulator”INTERACTIVE / LIVE
Live demoBuild a hash table
Change the bucket count, add your own IDs and watch collisions appear.
What happens during a collision?
Section titled “What happens during a collision?”A collision occurs when different keys map to the same bucket. Collisions are normal; they must be handled correctly. Common approaches include separate chaining and open addressing.
Complexity
Section titled “Complexity”| Operation | Average case | Worst case |
|---|---|---|
| Lookup | O(1) |
O(n) |
| Insert | O(1) |
O(n) |
| Delete | O(1) |
O(n) |