Evaluate Boolean Search Queries With Phrases Against a Document
Last updated: November 29, 2026
Quick Overview
Parse a query that combines words, quoted phrases, AND, OR, NOT, parentheses, and implicit AND between adjacent terms (for example: pixel "battery life" NOT (iphone OR galaxy)), and decide whether a given document's word sequence matches it.
0
0
0 solved
Parse a query that combines words, quoted phrases, AND, OR, NOT, parentheses, and implicit AND between adjacent terms (for example: pixel "battery life" NOT (iphone OR galaxy)), and decide whether a given document's word sequence matches it.
Gmail search, Drive search, and advanced Google Search operators all accept boolean queries, so building the parser and evaluator is a realistic onsite question. The interviewer probes operator precedence (NOT over AND over OR), implicit AND, phrase matching on positions, and clear syntax errors.
What the Interviewer Expects
- Defines precedence NOT over AND over OR and the implicit AND rule
- Writes a tokenizer that recognizes phrases, parentheses, and uppercase operators
- Builds an AST with recursive descent instead of evaluating on the fly
- Matches phrases by consecutive positions, not just presence of words
- Reports unbalanced parentheses and dangling operators
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Evaluate the query against an inverted index with posting lists instead of one document at a time.
- Which order should AND clauses be intersected in for speed, and why?
- How would you support NEAR/3 proximity operators?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Terms: bare words (case-insensitive) and
"quoted phrases". - Operators:
NOTbinds tightest, thenAND, thenOR; only uppercase words are operators, sooris a search term. - Adjacent terms ...
High-Level Design
Tokenize with one regex into (, ), AND/OR/NOT, ("WORD", w), and ("PHRASE", words). Parse with three functions following precedence. The interesting part is implicit AND: in and_expr, k...