Design a hash map
Writing a hash map from scratch is a common warm-up question. It is short enough to code in 20 minutes, and it shows whether you understand hashing, collisions and amortised cost. The same ideas come back at system scale in partitioning and sharding and in every cache.
Problem and constraints
Section titled “Problem and constraints”Implement a map with set(key, value), get(key) and remove(key). Before coding, ask:
- What types are the keys? The original version allowed only integers. We accept any hashable key and use Python’s
hash(). - How do we handle collisions? Chaining (a list per bucket) or open addressing (probe for the next free slot). Chaining is easier to get right on a whiteboard.
- Do we care about the load factor? The original said no and used a fixed-size table. That is the main gap: with 10 buckets and a million keys, every lookup scans a 100,000-entry list. We add resizing.
- What happens for a missing key? Raise
KeyError, likedict, rather than returnNone, which could also be a stored value. - Concurrency, persistence? Single-threaded, in memory.
Design
Section titled “Design”Entryholds one key and its value. The value is mutable soseton an existing key updates it in place.HashMapowns the buckets, the entry count and the maximum load factor. It exposesset,get,remove,len,inanditems. Two private helpers do the real work:_bucket_formaps a key to its bucket, and_resizebuilds a bigger array and rehashes into it.
classDiagram
class HashMap {
-buckets: list
-size: int
-max_load_factor: float
+capacity: int
+load_factor: float
+set(key, value)
+get(key) value
+remove(key) value
+items() Iterator
+\_\_len\_\_() int
+\_\_contains\_\_(key) bool
-bucket_for(key) list
-resize(new_capacity)
}
class Entry {
+key
+value
}
HashMap "1" *-- "0..*" Entry : buckets hold
The map composes its entries: they have no life outside it. The index depends on the capacity, so a resize cannot just copy the old buckets into a longer list. Every entry has to be hashed again into its new position.
Why chaining? Deleting an entry is a plain list removal. You need no “tombstone” markers, and the table still works, only slower, if the load factor goes above 1. Open addressing stores entries in the array itself, which is friendlier to CPU caches. That is why CPython’s dict, Rust’s HashMap and Go maps since Go 1.24 (Swiss tables) all use it. Java’s HashMap uses chaining and turns very long chains into balanced trees.
Implementation
Section titled “Implementation”from collections.abc import Hashable, Iteratorfrom dataclasses import dataclass
@dataclass(slots=True)class Entry[K, V]: key: K value: V
class HashMap[K: Hashable, V]: """Hash table with separate chaining that grows when it gets too full."""
def __init__(self, capacity: int = 8, max_load_factor: float = 0.75) -> None: if capacity < 1 or max_load_factor <= 0: raise ValueError("capacity and max_load_factor must be positive") self._buckets: list[list[Entry[K, V]]] = [[] for _ in range(capacity)] self._size = 0 self._max_load_factor = max_load_factor
def __len__(self) -> int: return self._size
def __contains__(self, key: K) -> bool: return any(entry.key == key for entry in self._bucket_for(key))
@property def capacity(self) -> int: return len(self._buckets)
@property def load_factor(self) -> float: return self._size / self.capacity
def set(self, key: K, value: V) -> None: bucket = self._bucket_for(key) for entry in bucket: if entry.key == key: # existing key: overwrite in place entry.value = value return bucket.append(Entry(key, value)) self._size += 1 if self.load_factor > self._max_load_factor: self._resize(self.capacity * 2)
def get(self, key: K) -> V: for entry in self._bucket_for(key): if entry.key == key: return entry.value raise KeyError(key)
def remove(self, key: K) -> V: bucket = self._bucket_for(key) for i, entry in enumerate(bucket): if entry.key == key: del bucket[i] self._size -= 1 return entry.value raise KeyError(key)
def items(self) -> Iterator[tuple[K, V]]: for bucket in self._buckets: for entry in bucket: yield entry.key, entry.value
def _bucket_for(self, key: K) -> list[Entry[K, V]]: # hash() works for any hashable key; % keeps the index in range (also for negative hashes). return self._buckets[hash(key) % self.capacity]
def _resize(self, new_capacity: int) -> None: old_buckets = self._buckets self._buckets = [[] for _ in range(new_capacity)] for bucket in old_buckets: for entry in bucket: # rehash: the index depends on the capacity self._buckets[hash(entry.key) % new_capacity].append(entry)
if __name__ == "__main__": m: HashMap[str, int] = HashMap(capacity=4) for i, word in enumerate("abcdef"): m.set(word, i) m.set("a", 100) # overwrite, size unchanged assert m.get("a") == 100 and len(m) == 6 assert m.capacity == 8 # grew 4 -> 8 when the 4th key arrived assert m.remove("c") == 2 and "c" not in m and len(m) == 5 print(sorted(m.items()))Complexity and trade-offs
Section titled “Complexity and trade-offs”| Operation | Average | Worst case |
|---|---|---|
get, set, remove | O(1) | O(n): all keys in one bucket |
| Resize | Amortised O(1) per insert | O(n) for the insert that triggers it |
| Memory | O(n + capacity) | — |
The average case assumes the hash spreads keys evenly. Then the expected chain length equals the load factor, which stays below 0.75. Two things break that assumption:
- Weak hash plus modulo. In Python, small integers hash to themselves, so keys
0, 8, 16, …all land in bucket 0 of an 8-bucket table. Production tables scramble the hash bits before taking the index, or use a prime capacity. - Hash flooding. An attacker who knows the hash function can send thousands of colliding keys and turn every request into an
O(n)scan. Python defends against this with a keyed, randomised hash forstrandbytes(SipHash). Java defends by turning long chains into trees.
| Separate chaining | Open addressing | |
|---|---|---|
| Delete | Simple list removal | Needs tombstones or back-shifting |
| Load factor above 1 | Still works, only slower | Impossible; resize before ~0.9 |
| CPU cache behaviour | Pointer chasing | Contiguous probes, faster |
| Implementation risk | Low | Probe sequence and clustering bugs |
Extensions
Section titled “Extensions”- Shrinking. Halve the capacity when the load factor drops below 0.25. Use a gap between the grow and shrink thresholds so a map hovering at one size does not resize on every call.
- Latency spikes during resize. A resize of 10 million entries blocks one unlucky insert. Redis avoids this with incremental rehashing: it keeps the old and new tables side by side and moves a few buckets on every operation.
- Thread safety. One lock around the map is simplest. Lock striping (a lock per group of buckets) lets writers to different buckets proceed in parallel. Java’s
ConcurrentHashMapgoes further: compare-and-swap for empty bins and a lock per bin otherwise. - Ordered iteration. If callers need insertion order, keep a separate array of entries and store indices in the buckets. CPython’s
dicthas done this since 3.6. - Beyond one machine. Choosing a server for a key is the same
hash % nproblem. Adding a server would move almost every key, which is why distributed caches use consistent hashing.
Related topics
Section titled “Related topics”- Design an LRU cache: a hash map plus a linked list.
- Partitioning and sharding: hashing keys across machines and consistent hashing.
- Cache strategies: where in-memory maps sit in a real system.
Based on The System Design Primer by Donne Martin (CC BY 4.0). Restructured, rewritten and updated; site content is licensed under CC BY 4.0.