Bloom filter

Write codeMedium28 minGoogle and Databricks style

Your task

Implement a Bloom filter with a bit array of size m and k hash functions. For a string s and hash index j (0-indexed), the j-th hash is h_j(s) = (sum((ord(c) * (j + 1) * (idx + 1)) for idx, c in enumerate(s)) * 2654435761 + j * 0x9E3779B1) % m, which sets/checks bit h_j(s). Given m, k, a list of strings to insert, and a list of query strings, return for each query 0 if the filter definitely does NOT contain it (at least one of its k bits is unset) or 1 if it might be present (all k bits set). Inserts are applied first, then queries. m is between 1 and 100000; k is between 1 and 16.

Examples

bloom_filter(m: int, k: int, inserts: list[str], queries: list[str]) -> list[int]
Example 1
Inputbloom_filter(1000, 3, ["apple", "banana"], ["apple", "cherry"])
Output[1, 0]