Sub topic 7
Choosing a Threshold and Triggering a Resize
This chapter assumes you know what a hash table is, can read simple Python, and understand Big-O basics. You should also know that the load factor is size divided by capacity, from the previous chapters.
The resize trigger
A hash table checks its load factor after each insertion. When the load factor goes above a fixed threshold, the table allocates a larger bucket array and moves every entry into it. The threshold is the maximum load factor, often called max_load.
The check has three parts:
- Insert the new entry and increase
size. - Compute
size / capacity. - If the result is greater than
max_load, call the resize routine.
Here is the trigger inside put from a separate-chaining table:
class ChainedHashTable:
def __init__(self, capacity=8, max_load=0.75):
self._buckets = [[] for _ in range(capacity)]
self._size = 0
self._max_load = max_load
def load_factor(self):
return self._size / len(self._buckets)
def _index(self, key):
return hash(key) % len(self._buckets)
def put(self, key, value):
bucket = self._buckets[self._index(key)]
for i, (k, _) in enumerate(bucket):
if k == key:
bucket[i] = (key, value)
return
bucket.append((key, value))
self._size += 1
if self.load_factor() > self._max_load:
self._resize(len(self._buckets) * 2)
def get(self, key, default=None):
for k, v in self._buckets[self._index(key)]:
if k == key:
return v
return default
def _resize(self, new_capacity):
old_buckets = self._buckets
self._buckets = [[] for _ in range(new_capacity)]
for bucket in old_buckets:
for key, value in bucket:
self._buckets[self._index(key)].append((key, value))Two details matter. First, replacing the value of an existing key returns early, so size does not change and no resize check runs. Only a new key can push the table over the threshold. Second, the check uses >, not >=. With capacity 8 and max_load of 0.75, the sixth entry gives 6/8 = 0.75, which does not trigger a resize. The seventh gives 7/8 = 0.875, which does.
How to choose the threshold
The threshold trades lookup time against memory. A low threshold keeps buckets short and lookups fast, but the table keeps many empty slots. A high threshold saves memory, but collisions become more frequent.
The best value depends on the collision strategy:
| Strategy | Typical threshold | Reason |
|---|---|---|
| Separate chaining | 0.75 to 1.0 | Chains just grow longer. Cost rises linearly with load, and the load factor can exceed 1. |
| Linear probing | 0.5 to 0.7 | Entries sit in the array itself. Probe sequences lengthen sharply as the load nears 1. |
Chaining tolerates a higher threshold because the expected cost of an unsuccessful lookup is about the load factor itself. Linear probing needs a lower one because its cost grows roughly with 1/(1 − load)², so it climbs steeply near full. The next chapters build on this.
Common errors
- Checking before the insert. If you test the load factor before adding the entry, the table can briefly exceed the threshold. It also resizes one insert late.
- Using integer division. In languages with integer division,
size / capacitygives 0 until the table is full. Convert to floating point first. Python 3's/already does this. - Resizing on updates. Overwriting an existing key does not grow the table, so it should not trigger a check.
- Reusing old indexes. The index is
hash(key) % capacity. After the capacity changes, every entry may belong in a different bucket, so you cannot just copy buckets across. The next chapter covers this in detail.
Exercise
A chained table has capacity 16 and maxload 0.75. It uses the check `loadfactor() > max_load` after each new key. How many entries can it hold before the next resize is triggered, and what is the load factor right after the resize if the capacity doubles?
Show answer
12 entries do not trigger a resize, since 12/16 = 0.75 is not greater than 0.75. The 13th entry gives 13/16 = 0.8125, which triggers the resize. After doubling to capacity 32, the load factor is 13/32 ≈ 0.406.
Exercise
Why would a table that uses linear probing usually choose a lower threshold than one that uses chaining?
Show answer
In linear probing, entries fill the array itself, and clusters of occupied slots form. The expected number of probes grows like 1/(1 − load)², so cost rises sharply near full. In chaining, the expected cost of an unsuccessful lookup grows only linearly, about equal to the load factor.