Back to Learning Area

Data Structures III (Key-Based)

Key-based data structures that inconspicuously power almost every system.

  • Fundamentals
  • 15 min
  • Intermediate

Plain Language

This preview is in Plain Language. The whole portal is also available in Plain Language. You can change the language at the top. Or use the button below. Our Plain Language texts are AI-assisted and editorially reviewed. They follow the core principles of easy-to-read language, but they are not certified Easy Read: a review panel of people with learning difficulties does not test them.

Preview: Finding data fast with keys

What are hash tables?

A hash table is a data structure. Data structure means: an order for data. A hash table finds data very fast. For this, it needs a key.

How the key works

You enter a key. This is, for example, a name. A hash function calculates a number from it. Hash function means: a calculation rule. This number shows the storage location of the data.

Why so fast?

With other methods, you have to search through lots of data. That takes a long time. A hash table calculates the position directly. This is possible in one step. No searching, no sorting.

Arrays give you O(1) access by position. Balanced binary search trees deliver O(log n) access by order. But what if you need to look up data by an arbitrary key — a username, a URL, or a session token? Hash tables complete the picture: they convert any key into an array position in constant time.

This article covers the three pillars of key-based data structures: the hash trick itself, the collision problem that every hash table must solve, and the caching principle that exploits the hash trick in practice.

Hash Tables: The O(1) Trick

Definition

Hash Table

Analogy

Imagine a huge library. You could search every shelf — O(n). Or use an alphabetical catalogue that halves the search space each time — O(log n), like a binary search tree. Or you whisper the title to a magical librarian who instantly tells you the exact shelf number — O(1). The librarian is the hash function: a mechanical rule that converts the title into a number without understanding it.

But beware: a real librarian recognises similarities and corrects typos. A hash function does not recognise similarity; many even turn the tiniest differences into completely different hash values (avalanche effect). And what happens when two titles map to the same shelf? The magical librarian never has this problem — hash tables do.

Example: Hash Calculation

Hash table with 10 slots:

hash("cat") = (99 + 97 + 116) % 10 = 312 % 10 = 2
hash("dog") = (100 + 111 + 103) % 10 = 314 % 10 = 4

Looking up "cat":
→ Compute hash → 2
→ Jump to slot 2
→ Done. One step instead of up to 10.

The ASCII values of the characters are summed and mapped to the table size using modulo. This gives you a direct address — no searching, no sorting, just compute and jump. This toy function is deliberately simple: "act" also sums to 312 and lands in slot 2 just like "cat"; real hash functions mix the characters far more thoroughly.

Array: O(1) by Index

Direct access via position number. Fast when you know the position — but you must know the number.

Hash Table: O(1) by Key

Direct access via any key (name, URL, ID). The hash function computes the position automatically.

Caution

Misconception: Hash Tables Are Always O(1)

O(1) is the average case with a good hash function and moderate load factor. In the worst case — many collisions, high load factor — access degrades to O(n). Rehashing helps against the high load factor: the table is enlarged and all entries are redistributed.

Collisions: When Two Keys Want the Same Slot

A collision occurs when two different keys hash to the same index. The pigeonhole principle guarantees this is unavoidable: if there are more possible keys than buckets, some must share. And collisions become likely long before the table is full: with ten randomly distributed keys in 100 slots, there is about a 37% chance of at least one collision (birthday paradox).

Imagine a coat check with 100 numbered hooks. The attendant assigns hooks based on the first letter of your last name — A gets hook 1, B gets hook 2. When two guests named "Smith" and "Sullivan" both map to hook 19, the attendant has two options: hang both coats on the same hook and search through them later (separate chaining), or put the second coat on the next free hook (open addressing).

A real coat-check attendant improvises when running out of hooks — stacking coats, hanging them on the door, or placing them on a chair. A hash table cannot improvise. It needs strict, pre-programmed rules (chaining or probing) for every collision.

Note

Collision Strategies

Separate Chaining Each bucket holds a linked list. On a collision, the new entry is appended to the list. Search: compute hash, then traverse the list in the bucket.
Open Addressing On a collision, the next free slot is found (linear probing, quadratic probing, or double hashing). All entries stay directly in the array.

Example: Collision in Practice

Hash table with 7 slots, h(key) = len(key) % 7:

"cat" → len=3 → 3 % 7 = 3
"dog" → len=3 → 3 % 7 = 3  ← Collision!
"elephant" → len=8 → 8 % 7 = 1
"bird" → len=4 → 4 % 7 = 4

Separate chaining at slot 3:
[("cat", val1) → ("dog", val2)]

"cat" and "dog" both have 3 characters — their hashes collide at slot 3. With separate chaining, the slot holds a linked list of both entries. Searching for "dog" requires 2 comparisons instead of 1, but remains fast. As the load factor rises, chains grow — at 75%, rehashing is typically triggered.

Note

Rehashing: When the Table Gets Full

Before and After: Rehashing

Before rehashing: 75% load factor, chains getting longer, search getting slower
After rehashing: doubled size, short chains, fast search restored

Caution

Misconception: Collisions Mean the Hash Table Is Broken

Collisions are mathematically guaranteed by the pigeonhole principle. A well-designed hash table handles them efficiently. It only becomes problematic if the hash function distributes poorly or the load factor rises too high without rehashing.

Interactive: Hash Demonstrator

Enter a key and watch how the hash function computes a slot number from it. Insert multiple keys and experience collisions live — when two different keys map to the same slot.

Controls
Hash table
0
—
1
—
2
—
3
—
4
—
5
—
6
—
7
—
8
—
9
—

Caching: Trading Memory for Speed

Caching stores frequently or recently accessed data in fast memory to avoid slow re-computation or network calls. The key terms: cache hit (data found locally — fast), cache miss (data not in cache — must fetch from slow source), and cache invalidation (removing stale entries).

The pizza-in-the-fridge analogy: Ordering pizza from a delivery service takes 30-40 minutes (fetching from the original source). Reheating leftover pizza from the fridge takes minutes (cache hit). But it might be stale the next day (stale data). You must decide when to throw it out and order fresh (cache invalidation / TTL).

In real life, pizza goes bad gradually — you notice by smell or taste. In a digital cache, there is no gradual decay: data is either valid or stale, with no in-between. That is why invalidation must be controlled by exact metadata like max-age, not by intuition.

Note

Memory Hierarchy: Latency Comparison

  1. 1. L1 Cache: ~1 nsfastest, smallest memory
  2. 2. L2 Cache: ~5 ns
  3. 3. L3 Cache: ~20 ns
  4. 4. RAM: ~100 ns
  5. 5. SSD: ~100 μs1,000× slower than RAM
  6. 6. Internet over long distances: ~100 ms1,000,000× slower than RAM

Real-World Example: Browser Caching

First visit: the browser loads HTML, CSS, and images from the server (~2 seconds). The server sends Cache-Control: max-age=3600. Second visit within one hour: the browser serves from local cache (~50 ms). After one hour: cache expired, the browser checks with the server and only re-downloads what has changed. Result: noticeably faster perceived load time for returning visitors.

Caution

Misconception: Caching Always Makes Everything Faster

Caching only helps when data is accessed repeatedly. For one-time queries, cache misses dominate and the cache just wastes memory. Also, aggressive caching without proper invalidation serves stale data. Hence the famous saying: "The two hard things in CS: cache invalidation and naming things."

In large language models (LLMs), hash tables are used for token-to-ID mapping: every word or subword is mapped to an integer that serves as an index into the embedding matrix. Hashing also plays a central role in data deduplication — identical training data is identified and filtered via its hash value. The KV cache (key-value cache) in transformer models is, despite its name, not a hash table but an example of the caching principle from this article: the already computed key and value vectors of earlier tokens are cached to avoid re-computation during token-by-token generation.

Note

Key Takeaways

  1. A hash function converts any key into an array index — that is the "magic trick" behind average O(1) lookup in dictionaries, maps, and caches.
  2. Caching trades memory for speed: store the answer once, skip the expensive computation or network call next time — but stale data is the price without proper invalidation.
  3. Collisions are not bugs — they are mathematically inevitable. Good hash tables handle them gracefully through chaining or probing and keep performance stable via rehashing.

Check

Quiz: Key-Based Data Structures

Not completed

What does a hash function do?

Select one answer

Answer Key: 1) B · 2) C · 3) B · 4) B

Check

Checkpoint: Key-Based Data Structures

Can you explain this in your own words? Tick what you've got.

0 of 3 done