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.

AdvancedCollision resolutionHash-table design

PROBLEM UNDERSTANDING

Input and expected output

Sample input
No input required
Sample output
data:Maybe graph:No

COMPLETE C PROGRAM

Complete C implementation

ads-bloom-filter-membership.c
Open in compiler
#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;
}

GUIDED CODE TOUR • NOT LIVE EXECUTION

Study the program line by line

Use the real compiler button above to run and debug with different inputs.

CURRENT STEP

Select Start to walk through the important lines.

SELECTED LINE

No line selected

EXPECTED OUTPUT FOR THE SAMPLE

data:Maybe graph:No
0%Step 0 of 0

PROGRAM EXPLANATION

Algorithm and explanation

  1. Read the required input values.
  2. Set several hashed bit positions; a missing bit proves absence while all set bits mean possible presence.
  3. 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.