Load factor and rehashing in Data Structures Theory - Time & Space Complexity
When using hash tables, it is important to understand how the load factor and rehashing affect performance.
We want to know how the time to insert or find items changes as the table fills up and grows.
Analyze the time complexity of this simplified hash table insertion with rehashing.
function insert(key, value) {
if (loadFactor() > 0.7) {
rehash();
}
index = hash(key) % tableSize;
table[index] = {key: key, value: value};
numberOfItems++;
}
function loadFactor() {
return numberOfItems / tableSize;
}
function rehash() {
oldTable = table;
tableSize = tableSize * 2;
table = new Array(tableSize);
numberOfItems = 0;
for (let item of oldTable) {
if (item !== undefined) {
insert(item.key, item.value);
}
}
}
This code inserts items into a hash table, and when the table is more than 70% full, it doubles the size and re-inserts all items.
Look at what repeats as the input grows.
- Primary operation: Inserting items into the hash table.
- How many times: Each insert is usually quick, but when rehashing happens, all existing items are re-inserted once.
Normally, inserting one item takes about the same time regardless of size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 simple inserts, occasional rehashing |
| 100 | About 100 inserts, with a few rehashes doubling the table |
| 1000 | About 1000 inserts, with several rehashes each re-inserting many items |
As the table grows, rehashing happens less often but costs more each time because it re-inserts all items.
Time Complexity: O(n)
This means that inserting n items takes time proportional to n, even with rehashing included.
[X] Wrong: "Rehashing makes insertion very slow every time."
[OK] Correct: Rehashing happens only occasionally, so its cost spreads out, keeping average insertion fast.
Understanding load factor and rehashing helps you explain how hash tables stay efficient as they grow, a key skill in many coding discussions.
"What if the load factor threshold was set to 0.9 instead of 0.7? How would that affect the time complexity?"
Practice
load factor in a hash table represent?Solution
Step 1: Understand load factor definition
Load factor is defined as the number of elements divided by the total slots in the hash table.Step 2: Match definition to options
The ratio of stored elements to the total capacity of the table correctly describes this ratio, while others describe unrelated concepts.Final Answer:
The ratio of stored elements to the total capacity of the table -> Option AQuick Check:
Load factor = elements / capacity [OK]
- Confusing load factor with number of collisions
- Thinking load factor measures speed
- Mixing load factor with element size
Solution
Step 1: Identify what happens when load factor is high
A high load factor means the table is too full, causing more collisions and slower operations.Step 2: Choose the correct response to high load factor
Rehashing by increasing the table size reduces load factor and improves performance, which matches Rehash the table by increasing its size.Final Answer:
Rehash the table by increasing its size -> Option CQuick Check:
High load factor -> rehash with bigger table [OK]
- Deleting elements randomly instead of resizing
- Ignoring the problem causing slowdowns
- Reducing table size which worsens collisions
Solution
Step 1: Calculate load factor after 7 elements
Load factor = 7 / 10 = 0.7, which equals the threshold.Step 2: Calculate load factor after 8 elements
Load factor = 8 / 10 = 0.8, which exceeds the threshold of 0.7, triggering rehashing.Final Answer:
Rehashing occurs because load factor exceeds threshold -> Option BQuick Check:
Load factor > threshold -> rehash [OK]
- Assuming threshold is inclusive and no rehash needed
- Thinking table shrinks instead of grows
- Believing insertion is rejected
Solution
Step 1: Understand effect of no rehashing at high load
Without rehashing, the table stays full, causing many collisions.Step 2: Identify impact on operations
More collisions slow down insertions and lookups, matching Insertions and lookups will become slower due to collisions.Final Answer:
Insertions and lookups will become slower due to collisions -> Option AQuick Check:
No rehash -> more collisions -> slower operations [OK]
- Thinking memory runs out immediately
- Assuming table shrinks automatically
- Believing elements get deleted automatically
Solution
Step 1: Calculate load factor before rehashing
Load factor = 12 / 16 = 0.75, which triggers rehashing.Step 2: Calculate load factor after doubling capacity
New capacity = 16 * 2 = 32. Load factor = 12 / 32 = 0.375 immediately after rehash.Final Answer:
0.375 -> Option DQuick Check:
Load factor after resize = elements / new capacity = 12/32 = 0.375 [OK]
- Using old capacity instead of new capacity
- Multiplying load factor by 2 incorrectly
- Confusing load factor with threshold
Ready to prove it?
Pass the test and this pattern moves unknown
