Computer Science I / Hashing and Dictionaries
Practice question · Put in order

Order the steps a hash table takes to look up a value by its key, using chaining.

Hints
  1. The lookup never scans the table; the index must be computed before anything is read.
  2. The list search only exists because collisions put several keys in one bucket.
Show the answer
  1. Apply the hash function to the key to get an integer
  2. Reduce that integer modulo the number of buckets to get an index
  3. Jump straight to the bucket at that index
  4. Search the short list held at that bucket for the matching key
  5. Return the value stored alongside that key
Why

Hashing and the modulo give an index with no memory reads at all, which is why the jump is direct; only then is the bucket's short chain searched. The whole cost is that final search, so keeping chains short, which is what the load factor and resizing control, is what keeps lookup constant on average.

Read the lesson: Hashing and Dictionaries →

Practise Hashing and Dictionaries

The app has 6 more questions on this lesson, and keeps your place in the course. Computer Science I is free to start.

More questions on Hashing and Dictionaries