ADVANCED DATA STRUCTURES PROGRAM • LEVEL 18 — ADVANCED HASHING
Equalize Probe Distances with Robin Hood Hashing
Learn how to equalize probe distances with robin hood hashing using a clear C program.
PROBLEM UNDERSTANDING
Input and expected output
Sample input
No input required
Sample output
3:10(0) 4:17(1) 5:24(2) 6:31(3)
COMPLETE C PROGRAM
Complete C implementation
#include <stdio.h>
int main(void)
{
int table[7]={0},distance[7]={0},keys[]={10,17,24,31};
for(int k=0;k<4;k++){int key=keys[k],dist=0,slot=key%7;while(table[slot]){if(distance[slot]<dist){int t=table[slot];table[slot]=key;key=t;t=distance[slot];distance[slot]=dist;dist=t;}slot=(slot+1)%7;dist++;}table[slot]=key;distance[slot]=dist;}
for(int i=0;i<7;i++){if(table[i])printf("%d:%d(%d) ",i,table[i],distance[i]);}
putchar('\n');return 0;
}CURRENT STEP
SELECTED LINE
EXPECTED OUTPUT FOR THE SAMPLE
3:10(0) 4:17(1) 5:24(2) 6:31(3)
Step 0 of 0
PROGRAM EXPLANATION
Algorithm and explanation
- Read the required input values.
- Let a farther-travelled key steal a slot from a key with a smaller probe distance.
- Display the computed result.
Let a farther-travelled key steal a slot from a key with a smaller probe distance.
EFFICIENCY
Time and space complexity
Time complexity
Average O(1)
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.
