Back to DAG

Hash Table

databases

What is a Hash Table?

A hash table is a data structure that maps keys to values using a hash function. The hash function converts a key into an index in an underlying array (called buckets), enabling average-case O(1) lookup, insertion, and deletion.

Core idea

  1. Hash function — takes a key and produces an integer. A good hash function distributes keys uniformly across buckets.
  2. Bucket array — a fixed-size array where each slot holds zero or more entries.
  3. Collision resolution — when two keys hash to the same bucket, we need a strategy. The two most common are:
    • Chaining: each bucket holds a linked list of entries.
    • Open addressing: probe for the next available slot (linear probing, quadratic probing, double hashing).

Complexity

OperationAverageWorst case (all keys collide)
GetO(1)O(n)
PutO(1)O(n)
RemoveO(1)O(n)

The worst case happens when every key hashes to the same bucket, degenerating the table into a linked list.

Load factor and resizing

The load factor = (number of entries) / (number of buckets). When it exceeds a threshold (commonly 0.75), the table resizes — typically doubling the bucket array and rehashing all entries. This keeps the average chain length short.

Real-Life: Phone Book Lookup

Real-World Example

Think of a phone book organized by the first letter of each last name. The "hash function" is taking the first letter: all names starting with "S" go into the S section. Within that section, you scan linearly — that's chaining.

If most people in your town have last names starting with "S", the S section becomes huge while other sections are nearly empty — that's a bad hash function causing uneven distribution.

In databases, hash tables power:

  • Hash indexes — PostgreSQL hash indexes for equality lookups
  • Hash joins — the database builds a hash table on the smaller relation, then probes it with the larger relation
  • In-memory caches — Redis is essentially a networked hash table

Hash Table with Chaining

Buckets 0 "apple"→ 5 1 null 2 "cat"→ 12 "dog"→ 3 3 "egg"→ 7 4 null hash("cat") = hash("dog") = 2 → collision resolved by chaining

Interactive Hash Table

Loading demo...
Step 1 of 3