ADVANCED DATA STRUCTURES PROGRAM • LEVEL 18 — ADVANCED HASHING
Rehash a Table When Its Load Factor Grows
Learn how to rehash a table when its load factor grows using a clear C program.
PROBLEM UNDERSTANDING
Input and expected output
Sample input
No input required
Sample output
0:11 1:1 5:16 6:6
COMPLETE C PROGRAM
Complete C implementation
#include <stdio.h>
int main(void)
{
int old[5]={0};int keys[]={1,6,11,16};for(int i=0;i<4;i++){int slot=keys[i]%5;while(old[slot])slot=(slot+1)%5;old[slot]=keys[i];}int fresh[11]={0};for(int i=0;i<5;i++)if(old[i]){int slot=old[i]%11;while(fresh[slot])slot=(slot+1)%11;fresh[slot]=old[i];}for(int i=0;i<11;i++)if(fresh[i])printf("%d:%d ",i,fresh[i]);putchar('\n');return 0;
}CURRENT STEP
SELECTED LINE
EXPECTED OUTPUT FOR THE SAMPLE
0:11 1:1 5:16 6:6
Step 0 of 0
PROGRAM EXPLANATION
Algorithm and explanation
- Read the required input values.
- Allocate a larger table and insert every active key using the new modulus.
- Display the computed result.
Allocate a larger table and insert every active key using the new modulus.
EFFICIENCY
Time and space complexity
Time complexity
O(n) rehash
Auxiliary space
O(table size)
DEBUGGING CHECKLIST
Common mistakes
Check this
Use the correct format specifier for every variable.
Check this
Initialize variables before using their values.
Check this
Check braces, semicolons and input order carefully.
Try it yourself
Practice: Run the program with the sample input, predict its output, and then test one boundary case of your own.
