ADVANCED DATA STRUCTURES PROGRAM • LEVEL 18 — ADVANCED HASHING
Test Probabilistic Membership with a Bloom Filter
Learn how to test probabilistic membership with a bloom filter using a clear C program.
PROBLEM UNDERSTANDING
Input and expected output
Sample input
No input required
Sample output
data:Maybe graph:No
COMPLETE C PROGRAM
Complete C implementation
#include <stdio.h>
unsigned int hash1(const char*s){unsigned int h=5381;while(*s)h=h*33u+(unsigned char)*s++;return h;}unsigned int hash2(const char*s){unsigned int h=0;while(*s)h=h*131u+(unsigned char)*s++;return h;}
int main(void)
{
unsigned int bits=0;const char*words[]={"code","data","tree"};for(int i=0;i<3;i++){bits|=1u<<(hash1(words[i])%32);bits|=1u<<(hash2(words[i])%32);}const char*queries[]={"data","graph"};for(int i=0;i<2;i++){int maybe=(bits&(1u<<(hash1(queries[i])%32)))&&(bits&(1u<<(hash2(queries[i])%32)));printf("%s:%s%c",queries[i],maybe?"Maybe":"No",i==1?'\n':' ');}return 0;
}CURRENT STEP
SELECTED LINE
EXPECTED OUTPUT FOR THE SAMPLE
data:Maybe graph:No
Step 0 of 0
PROGRAM EXPLANATION
Algorithm and explanation
- Read the required input values.
- Set several hashed bit positions; a missing bit proves absence while all set bits mean possible presence.
- Display the computed result.
Set several hashed bit positions; a missing bit proves absence while all set bits mean possible presence.
EFFICIENCY
Time and space complexity
Time complexity
O(k) per operation
Auxiliary space
O(bit-array 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.
