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.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

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
Recursive Descent Parsing
Abstract Syntax Tree
Boolean Retrieval
Phrase Matching
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample Answer
Requirements
  • Terms: bare words (case-insensitive) and "quoted phrases".
  • Operators: NOT binds tightest, then AND, then OR; only uppercase words are operators, so or is 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...


Submit Your Answer
Markdown supported