Bloom Filter

medium · probabilistic, hashing

Bloom Filter

Implement a Bloom filter that supports membership checks with possible false positives.

A Bloom filter is a bitset plus k hash functions. To add a value, hash it k times and set those bit positions. To test membership, check that all positions are set.

API

type BloomFilter struct { /* ... */ }

func NewBloomFilter(size int, k int) *BloomFilter
func (bf *BloomFilter) Add(value string)
func (bf *BloomFilter) Contains(value string) bool

Example

bf := NewBloomFilter(128, 3)
bf.Add("apple")
Contains("apple") == true

Notes

  • Contains may return true for values never added (false positives).
  • Use the provided hash64 helper (or your own) to generate hashes.
Run tests to see results
No issues detected