BeginnerTopic 21 of 104Software & Computing

Hash tables

Map keys to values in average O(1) time using hash functions.

In depth

A hash table maps keys to array slots via a hash function; collisions are resolved by chaining or open addressing. Average O(1) insert, lookup and delete; load factor triggers resizing. Good hash functions distribute keys uniformly. Python dicts and Java HashMaps use them.

Key points

  • Collision resolution
  • Load factor ~0.7
  • Hash flooding attacks
Example

A word-frequency counter uses a dictionary from word to count.

Why it matters

Hash tables give very fast lookup by key, which is why they are used in caches, symbol tables and indexes.

Where this is used

  • Counting word frequencies in a document.
  • Storing user settings by name in an application.
  • Checking if a device ID has already been seen in a stream.

Frequently asked questions

What is Hash tables?

Map keys to values in average O(1) time using hash functions.

Why is it important to understand hash tables?

Hash tables give very fast lookup by key, which is why they are used in caches, symbol tables and indexes.

What happens when two keys hash to the same slot?

This is a collision, handled by chaining or open addressing, and too many collisions slow the table down.

Why is average O(1) not guaranteed?

Worst-case collisions can make lookups slow, and resizing the table takes time, so the O(1) bound is an average over typical inputs.

How is Hash tables used in practice?

Typical applications include counting word frequencies in a document, storing user settings by name in an application and checking if a device ID has already been seen in a stream.

What should I learn before Hash tables?

Study "Linked lists" first (topic 20 of 104 in Software & Computing). It covers the groundwork this topic builds on.

What are the key points of Hash tables?

Collision resolution. Load factor ~0.7. Hash flooding attacks.

Can you give an example of Hash tables?

A word-frequency counter uses a dictionary from word to count.

What comes after Hash tables?

The next topic is "Trees", which builds directly on this one.

How difficult is Hash tables?

It is classed as Beginner in Software & Computing (topic 21 of 104). It is an entry-level topic. It relies only on basic school mathematics and the ideas introduced earlier in this discipline.

Learning path

Want to practise? Open the interactive course for quick checks, quizzes, simulations and progress tracking.

Related topics