Tokenize a Search Query With Phrases, Negation, and site: Operators

Last updated: November 29, 2026

Quick Overview

Write a tokenizer that turns a raw search box string into a list of terms: plain words, quoted phrases, and operators like site:, filetype:, and intitle:, each possibly negated by a leading minus sign, while tolerating unterminated quotes.

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

0

0

0 solved


Write a tokenizer that turns a raw search box string into a list of terms: plain words, quoted phrases, and operators like site:, filetype:, and intitle:, each possibly negated by a leading minus sign, while tolerating unterminated quotes.

A Google Search flavored onsite question about the first stage of query understanding. The interviewer is checking that you write a hand-rolled scanner with clear precedence rules, and that you handle real user input such as hyphenated words, URLs with colons, and a missing closing quote.

What the Interviewer Expects
  • Defines token types and precedence before coding
  • Scans with an index instead of relying on split
  • Recognizes negation only when the minus starts a token and is followed by a non-space
  • Treats only a whitelist of prefixes as operators so URLs stay plain words
  • Handles unterminated quotes gracefully
Key Topics to Cover
Lexical Analysis
String Parsing
Search Query Understanding
Robustness
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
  • Add OR and parentheses and produce a boolean expression tree.
  • How would you tokenize queries in Chinese or Japanese where there are no spaces?
  • Where should spelling correction run relative to this tokenizer?
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
  • Input: a query like pixel 9 "battery life" -"pro max" site:reddit.com -case filetype:PDF.
  • Output: list of dicts with type in word, phrase, operator, plus value and negated.
  • A - n...
High-Level Design

Use a manual scanner with index i. Skip whitespace. If the current character is - followed by a non-space, set negated and advance. If it is a quote, find the closing quote (or use end of input)...


Submit Your Answer
Markdown supported