New positions after resize
Warm-up · Trace the algorithm · about 5 min
A chaining table starts with 4 buckets and uses key mod m, where m is the current number of buckets. It doubles when keys ÷ buckets goes above 0.75. Insert 5, 9, 2, 14. The fourth insert triggers a resize to 8 buckets. Give the new bucket of each key.
Example 1
Input:
{"size":4,"keys":[5,9,2,14]}
Output:
[[],[9],[2],[],[],[5],[14],[]]
Before the resize, 5 and 9 share bucket 1, and 2 and 14 share bucket 2.
Constraints
- The table starts with 4 buckets, hash = key mod m
- It doubles when keys ÷ buckets > 0.75
- Every key is rehashed with the new m
Learn the idea first: Load factor and resizing, a visual lesson.
More Load factor and resizing problems
- Choose the threshold Warm-up
- Amortized cost Standard
- Count the resizes Standard
- Total rehash moves Standard
- Implement resize Challenge