Hash Maps From the Inside

"Explain how a hash map works" is a depth probe at half the top trading firms, asked in conversation rather than as a coding problem. It is asked because the honest answer has four parts and most candidates give one.

The four parts

1. A hash function maps a key to an integer, ideally spreading similar keys far apart. 2. A bucket index is that integer reduced modulo the table size. 3. A collision policy decides what happens when two keys land in the same bucket, which they will. 4. A growth policy decides when the table is too full and rebuilds it larger.

Lookup is constant time on average because the first two steps are constant and the third is expected to inspect a small number of entries. It is linear in the worst case, when every key collides. Both halves of that sentence matter in an interview.

The rest of this lesson is for subscribers

Unlock every lesson in Programming for Quantitative Developers, and every other premium course.

Subscribe to continue

Test your knowledge

Questions are only available to subscribers.

Keep reading Programming for Quantitative Developers

25 lessons in this course, and every other premium course, on one subscription.

  • Every lesson in every course, with the worked examples and interactive simulators
  • Graded questions on every lesson, with explanations for the wrong answers as well as the right one
  • The trainers, timed assessments and brainteaser library that go with them