Snowflake questions

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.

  1. 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 || c reads as (a && b) || c, which is true. c || a && c reads as c || (a && c), which is false. a || b && c reads as a || (b && c), which is true. The parenthesized forms and the space-free a&&c parse the same way. The missing file nodoc has no words, so a || b is false.

Constraints

  • 1 <= len(operations) <= 10^4
  • 1 <= len(filename) <= 10
  • 0 <= len(content) <= 10^3
  • 1 <= len(predicate) <= 10^3
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question