ADVANCED DATA STRUCTURES PROGRAM • LEVEL 18 — ADVANCED HASHING
Resolve Collisions with Double Hashing
Learn how to resolve collisions with double hashing using a clear C program.
PROBLEM UNDERSTANDING
Input and expected output
Sample input
No input required
Sample output
2:41 5:18 9:22 10:44
COMPLETE C PROGRAM
Complete C implementation
#include <stdio.h>
int main(void)
{
int table[13];for(int i=0;i<13;i++)table[i]=-1;int keys[]={18,41,22,44};
for(int k=0;k<4;k++){int h1=keys[k]%13,h2=7-keys[k]%7;for(int step=0;step<13;step++){int slot=(h1+step*h2)%13;if(table[slot]<0){table[slot]=keys[k];break;}}}
for(int i=0;i<13;i++){if(table[i]>=0)printf("%d:%d ",i,table[i]);}
putchar('\n');return 0;
}CURRENT STEP
SELECTED LINE
EXPECTED OUTPUT FOR THE SAMPLE
2:41 5:18 9:22 10:44
Step 0 of 0
PROGRAM EXPLANATION
Algorithm and explanation
- Read the required input values.
- Use a second hash as the probe step so colliding keys follow different sequences.
- Display the computed result.
Use a second hash as the probe step so colliding keys follow different sequences.
EFFICIENCY
Time and space complexity
Time complexity
Average O(1), worst O(n)
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.
