0
0
Data Structures Theoryknowledge~10 mins

Load factor and rehashing in Data Structures Theory - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Load factor and rehashing
Start with empty hash table
Insert new key
Calculate load factor
Is load factor > threshold?
NoContinue inserting
Yes
Trigger rehashing
Create larger table
Re-insert all keys
Resume insertions
This flow shows how inserting keys updates the load factor, and when it exceeds a limit, triggers rehashing to a bigger table.
Execution Sample
Data Structures Theory
Insert keys: A, B, C, D
Load factor threshold = 0.75
Table size starts at 4
Rehash when load factor > 0.75
We insert keys one by one, check load factor after each, and rehash when needed.
Analysis Table
StepKeys InsertedTable SizeLoad FactorAction
1A40.25 (1/4)Insert key A
2A, B40.5 (2/4)Insert key B
3A, B, C40.75 (3/4)Insert key C
4A, B, C, D41.0 (4/4)Load factor > 0.75, trigger rehash
5A, B, C, D80.5 (4/8)Rehash to bigger table, re-insert keys
6A, B, C, D, E80.625 (5/8)Insert key E
7A, B, C, D, E, F80.75 (6/8)Insert key F
8A, B, C, D, E, F, G80.875 (7/8)Load factor > 0.75, trigger rehash
9A, B, C, D, E, F, G160.4375 (7/16)Rehash to bigger table, re-insert keys
10A, B, C, D, E, F, G, H160.5 (8/16)Insert key H
💡 Process stops after inserting key H with load factor below threshold.
State Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6After Step 7After Step 8After Step 9After Step 10
Keys Inserted[][A][A, B][A, B, C][A, B, C, D][A, B, C, D][A, B, C, D, E][A, B, C, D, E, F][A, B, C, D, E, F, G][A, B, C, D, E, F, G][A, B, C, D, E, F, G, H]
Table Size4444488881616
Load Factor00.250.50.751.00.50.6250.750.8750.43750.5
Key Insights - 3 Insights
Why does the load factor become 1.0 at step 4 even though the threshold is 0.75?
At step 4, inserting the 4th key fills the table completely (4 keys in size 4), so load factor is 4/4 = 1.0, which is above the 0.75 threshold, triggering rehashing as shown in execution_table row 4.
What happens to the keys during rehashing at step 5?
During rehashing, the table size doubles from 4 to 8, and all existing keys are re-inserted into the new larger table, reducing the load factor to 0.5 as shown in execution_table row 5.
Why does rehashing happen again at step 8?
At step 8, inserting the 7th key into a table of size 8 results in load factor 7/8 = 0.875, which exceeds the 0.75 threshold, so rehashing is triggered again as shown in execution_table row 8.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 4. What is the load factor and what action is taken?
ALoad factor is 1.0; rehashing is triggered
BLoad factor is 0.75; continue inserting
CLoad factor is 0.5; no action
DLoad factor is 0.25; rehashing is triggered
💡 Hint
Check the 'Load Factor' and 'Action' columns at step 4 in the execution_table.
At which step does the table size first increase from 4 to 8?
AStep 7
BStep 3
CStep 5
DStep 9
💡 Hint
Look at the 'Table Size' column in the variable_tracker after each step.
If the load factor threshold was changed to 0.9, at which step would the first rehash occur?
AStep 4
BStep 8
CStep 5
DStep 9
💡 Hint
Compare load factors in execution_table rows to the new threshold 0.9.
Concept Snapshot
Load factor = (number of keys) / (table size).
When load factor exceeds a threshold (e.g., 0.75), rehashing occurs.
Rehashing means creating a bigger table and re-inserting all keys.
This keeps the hash table efficient by reducing collisions.
Rehashing doubles the table size to lower load factor.
Insertions continue after rehashing with updated table size.
Full Transcript
This visual execution trace shows how load factor and rehashing work in a hash table. We start with an empty table of size 4. Each inserted key increases the load factor, calculated as keys divided by table size. When the load factor exceeds 0.75, rehashing triggers. Rehashing creates a new table twice as big and re-inserts all keys, lowering the load factor. This process repeats as more keys are added, keeping the table efficient. The execution table and variable tracker show step-by-step changes in keys, table size, and load factor, highlighting when rehashing happens.