Insert documents and check them against boolean predicates
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Snowflake's two coding screens round, last asked Apr 2026. It tests parsing, boolean expressions and api design.
Build a small in-memory document service. Documents are text, identified by filename. A predicate is a boolean expression over terms; a term is true when that exact word appears in the document. Predicates use && and ||, with && binding tighter than ||, and parentheses for grouping. Start from a blank editor: define the data structures and write your own tests.
Part 1: Insert and check
Unless you agree on a different interface with the interviewer, assume:
-
insert_doc(filename: str, content: str) -> None— stores the document, overwriting any earlier content. Content is words separated by whitespace. -
check_contains(filename: str, predicate: str) -> bool— whether the document satisfies the predicate. A missing file behaves like an empty document.
insert_doc("doc1", "a b")
check_contains("doc1", "a && b || c") -> True # (a && b) || c
check_contains("doc1", "a && (b || c)") -> True
check_contains("doc1", "c || d") -> False
check_contains("doc1", "b && c || a && d") -> False
check_contains("missing", "a") -> False
Part 2: Find all matching files
-
get_all_files(predicate: str) -> list[str]— every filename whose current content matches, sorted.
insert_doc("a.txt", "red blue")
insert_doc("b.txt", "blue green")
insert_doc("c.txt", "green")
get_all_files("red || green") -> ["a.txt", "b.txt", "c.txt"]
get_all_files("red && blue") -> ["a.txt"]
get_all_files("blue && (red || green)") -> ["a.txt", "b.txt"]
insert_doc("a.txt", "green") # overwrite
get_all_files("blue") -> ["b.txt"]
get_all_files("red") -> []
Part 3: Scale it out
Explain how you would run this service across many machines: how documents are sharded, how they are replicated, and how check_contains and get_all_files are served.
Constraints
- Up to
10^4operations in the coding parts; predicates are valid. - Words are exact, case-sensitive matches on whitespace-separated tokens.
Clarifying questions
- Are parentheses required, or only
&&and||? - Is matching on whole words, and is it case-sensitive?
- Is there a
!(not) operator? - How large are documents, and how many files are there?
What a strong answer covers
- Each document stored as a set of its words, so a term check is
O(1). - A tokenizer and a recursive-descent parser with one function per precedence level (
oroverandover a term or parenthesized group), or a shunting-yard conversion; parse once and evaluate the tree. - Part 2 with an inverted index from word to the set of filenames: a term becomes a set,
&&becomes intersection,||union. On overwrite, remove the file from the old words' sets first. - Costs: parsing
O(length of predicate), Part 1 evaluationO(terms), Part 2 proportional to the sizes of the sets combined. - Part 3: sharding by filename hash (each shard keeps its own index and
get_all_filesfans out and merges) versus by term (fast single-term queries but expensive multi-term ones); replication with a leader per shard or quorum writes, and reads from replicas. - Tests: precedence, parentheses, overwrite, a missing file, an empty document.
Follow-ups
- Add
!(not). What does it do to the inverted-index evaluation in Part 2? - Cache parsed predicates that are checked many times.
- A shard goes down during
get_all_files. What do you return?
Where it's asked
- Snowflake · Two coding screens · Last asked Apr 202660 min · Two back-to-back live coding rounds, often on the same day; some loops pair coding with system design · Varies: some screens forbid AI, some 2026 screens allow it. The scheduling email says.
What it tests
parsing · boolean expressions · api design
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
