Hash Tables

Hash tables: the data structure that turns a key into an array index to find values in expected constant time, and what collisions, growth and bad keys do to it.

  • 10 cards
  • 6 questions
  • About 31 min
  • Beginner to Intermediate
Share

A hash table turns a key into the index of an array slot, so it can store and find a value by its key without searching. It matters because that lookup, O(1) expected, is what makes dictionaries, sets, caches and counters fast in every language, and because the cases in which it is not O(1) are often caused by accident. The topic starts with the hash function and the bucket it picks, and the two ways to handle keys that land in the same bucket: chaining and linear probing. It then covers the load factor, the rehashing that growing needs, and a minimal map in Python, Java and TypeScript beside each language's built-in and the order it keeps. It ends with the two ways a lookup goes wrong: many keys in one bucket, and keys whose hash code or equality can change.

Contents

10 cardsnot started