DevPrep

All topics › Algorithms & data structures

What is a hash table and how are collisions resolved?

Middle PRO

The gist

A hash table stores key–value pairs in an array of buckets. The bucket index is computed from the key: hash(key) % capacity. This gives average O(1) lookup, insertion and removal — given a good hash function and a moderate load factor (elements / buckets).

The rest of this answer, with code and interview follow-ups, is in the DevPrep app.

Read the full answer in DevPrep

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store