Document Store with Predicates
SnowflakePhone screenMedium
Documents are identified by a file name and hold text made of whitespace-separated words. Word matching is exact and case-sensitive: a predicate word matches a document only if the document contains that exact word.
A predicate is built from words, &&, || and parentheses. && binds tighter than ||. Predicates are always valid, and operators and parentheses may or may not be surrounded by spaces.
Each test drives one DocStore through a sequence of calls and checks every return value.
Part 1: Check one document
Implement insert_doc(filename, content) and check_contains(filename, predicate).
insert_doc stores content as the document filename, replacing any earlier document with the same name, and returns nothing.
check_contains returns true if the document satisfies predicate, otherwise false. A file that was never inserted behaves like an empty document.
- Example 1
- init
[["n1","n2","n3"],4,2]- operations
[["insert_doc",["doc1","a b"]],["check_contains",["doc1","a && b || c"]],["check_contains",["doc1","c || a && c"]],["check_contains",["doc1","a || b && c"]],["check_contains",["doc1","(a || c) && (c || b)"]],["check_contains",["doc1","(a && c)||b"]],["check_contains",["nodoc","a || b"]],["check_contains",["doc1","a&&c"]]]- Output
[null,true,false,true,true,true,false,false]
Why: doc1 holds the words a and b.
a && b || creads as(a && b) || c, which is true.c || a && creads asc || (a && c), which is false.a || b && creads asa || (b && c), which is true. The parenthesized forms and the space-freea&&cparse the same way. The missing filenodochas no words, soa || bis false.
Constraints
1 <= len(operations) <= 10^41 <= len(filename) <= 100 <= len(content) <= 10^31 <= len(predicate) <= 10^3