What is a hash table?
Last updated: 2026-11-26
A hash table stores keys in an array and uses a hash function to compute, from each key, the array position where it belongs. To look a key up, you compute its position again and search there. Two keys can get the same position, which is called a collision, so a hash table also needs a rule for handling collisions.
Start learning No account needed
1. The two parts
The hash function turns a key into a slot number from 0 to M − 1, where M is the number of slots. A common one for whole numbers is the remainder k mod M. Equal keys must give the same result, and a good function spreads different keys across the slots.
Collision handling comes in two main families. With separate chaining, each slot holds a list, and colliding keys share it. With open addressing, the keys sit in the array itself, and a collision sends you to another position; linear probing tries the next one.
2. A worked example: eight keys, three table sizes
Use chaining and the hash function k mod M. The keys are 12, 24, 36 and so on up to 96. Predict how many slots they occupy when M is 12, 11 and 16. The numbers come from scripts/what-is-a-hash-table-worked-example.py in the Professor Turing repository. A comparison means one key checked while searching a chain from its start, and the average is over successful searches for the stored keys.
keys = 12, 24, 36, 48, 60, 72, 84, 96
M = 12: gcd(12, 12) = 12, slots used 0
longest chain 8, load factor 0.67, average comparisons 4.50
M = 11: gcd(12, 11) = 1, slots used 1, 2, 3, 4, 5, 6, 7, 8
longest chain 1, load factor 0.73, average comparisons 1.00
M = 16: gcd(12, 16) = 4, slots used 0, 4, 8, 12
longest chain 2, load factor 0.50, average comparisons 1.50
keys = 11, 22, 33, 44, 55, 66, 77, 88
M = 11: gcd(11, 11) = 11, slots used 0
longest chain 8, load factor 0.73, average comparisons 4.50
linear probing estimates under uniform hashing:
load 0.5: successful search about 1.5 probes, unsuccessful about 2.5
load 0.9: successful search about 5.5 probes, unsuccessful about 50.5
3. Why the table size mattered
Each key in the first set has the form 12i, and it lands in slot b when 12i leaves the same remainder as b on division by M. A standard number theory result says such an i exists exactly when the greatest common divisor of 12 and M divides b. With M = 12 that divisor is 12, so only slot 0 is reachable. With M = 16 it is 4, so the keys can reach slots 0, 4, 8 and 12. With M = 11 it is 1, so multiples of 12 can reach any slot.
Sedgewick and Wayne choose a prime M for this hash function because it spreads keys well. The divisor argument shows one pattern a prime helps with, regular spacing, and its limit: the second set of keys in the output is multiples of 11, and M = 11 puts them all in one slot. The result depends on the function and the keys together. Another approach is to choose the hash function at random from a family, as in the multiplicative hashing described by Open Data Structures.
4. When lookup is constant time
A search costs the time to compute the hash plus the time to search one chain. The load factor, the number of keys N divided by M, is the average chain length. An average does not tell you the length of the chain you need: in the first case the load factor is below 1 while one chain holds all eight keys.
Under the assumption that the hash function distributes keys uniformly, and with M grown as keys arrive so that N / M stays bounded, a search takes a constant number of comparisons on average. In the worst case, with all keys in one chain, it takes time proportional to N. Computing the hash also costs time. The string hash that Sedgewick and Wayne show loops over each character, so a longer string takes longer to hash.
5. Linear probing and load
With open addressing, the table cannot hold more keys than it has slots, and long runs of occupied slots make searches longer. Sedgewick and Wayne give approximate averages for linear probing under the uniform hashing assumption, with load factor a: about (1 + 1 / (1 − a)) / 2 probes for a successful search and (1 + 1 / (1 − a)2) / 2 for an unsuccessful search or an insertion. The output evaluates both at 0.5 and 0.9. Open Data Structures resizes its linear probing table as it fills, which keeps the load away from 1.
6. A common mistake: quoting O(1) with no conditions
"Hash tables are O(1)" leaves out what the statement rests on. Say which cost you mean (expected or worst case), what you assume about the hash function, and how the load is controlled. A one-line check: name an input that would put all your keys in one chain, and say what a search costs then.
7. Test yourself
Case 1. With M = 10 and k mod M, which slots can keys that are multiples of 4 reach?
Answer to case 1
The greatest common divisor of 4 and 10 is 2, so slots 0, 2, 4, 6 and 8.
Case 2. A chained table has 1,000 slots and 500 keys. Can a search still need 500 comparisons?
Answer to case 2
Yes, if all 500 keys hash to one slot. The load factor of 0.5 is an average over slots. A good hash function makes this outcome unlikely but the load factor alone does not rule it out.
Case 3. Insert 5, 15 and 25 into an empty table of size 10 with k mod 10 and linear probing. Where do they go?
Answer to case 3
All three hash to 5. They go to slots 5, 6 and 7.
For exam practice, see how to study for a computer science exam and how to prepare for a coding interview.
What the sources found, and their limits
Robert Sedgewick and Kevin Wayne, Algorithms, fourth edition, section 3.4. It covers modular hashing with a prime table size, the uniform hashing assumption, separate chaining, linear probing, load factor and the probe estimates. Source: Hash Tables, read on 2026-11-25.
Pat Morin, Open Data Structures, section 5.1. It describes chaining, says a bad hash function can put all values in one list, and analyses multiplicative hashing with a randomly chosen odd multiplier, bounding the expected length of the list for a value. Source: ChainedHashTable: Hashing with Chaining, read on 2026-11-25.
The same book, section 5.2. It analyzes linear probing through runs of occupied positions and resizes the array as elements are added. Source: LinearHashTable: Linear Probing, read on 2026-11-25.
William Stein, Elementary Number Theory: Primes, Congruences, and Secrets, section 2.1. Proposition 2.1.15 says that ax ≡ b (mod n) has a solution exactly when the greatest common divisor of a and n divides b. Source: Elementary Number Theory (PDF), read on 2026-11-25.
NIST Dictionary of Algorithms and Data Structures. It defines a hash table and a collision and names chaining and open addressing as resolution schemes. Source: hash table, read on 2026-11-25.
Limits: selected passages of three textbooks and a dictionary entry. The keys, the table sizes, the use of the number theory result on this hash function and the self-test cases are ours. Libraries differ in how their hash tables are built, and this page does not describe a particular one.
How Professor Turing helps
Professor Turing is a tutor for adults learning any subject, in college or on their own. It asks you questions instead of giving the final answer, does not grade a bare guess, and remembers what you got wrong. No account is needed to start. It's an AI tutor, free to try.
Bring a hash function and a few keys and say where you think they land. Its first reply asks what you already know, such as what happens on a collision, before it teaches anything. You must be 18 or older, and guest conversations are deleted 7 days after the last activity.
FAQ
Is hash table lookup O(1)? It is expected constant time under conditions: the hash function spreads the keys well and the number of keys per slot is kept bounded. If the keys pile into one chain, a search can take time proportional to the number of keys.
Does a prime table size prevent collisions? No. A prime size avoids shared factors between the key spacing and the table size unless the spacing is a multiple of that prime. In our example, keys that are multiples of 11 all land in one slot of a table of size 11.
Start with what you're working on
Tell the tutor which data structures question you are working on and let it ask you the questions.
Start learning No account needed
See also: how to study for a computer science exam · stack and queue · array and linked list · what is JSON · all study guides