Parse a Search Query with Phrases, Filters, and Exclusions

Last updated: November 29, 2026

Quick Overview

Write a parser for Pinterest search input that splits a raw query into free terms, quoted phrases, key:value filters, and excluded terms or phrases prefixed with a minus sign. It must be tolerant of messy input such as extra spaces and unclosed quotes.

Pinterest
Coding & Algorithms
Software Engineer
Pinterest
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Medium

0

0

0 solved


Write a parser for Pinterest search input that splits a raw query into free terms, quoted phrases, key:value filters, and excluded terms or phrases prefixed with a minus sign. It must be tolerant of messy input such as extra spaces and unclosed quotes.

Search on Pinterest supports shopping filters and phrases, and the query understanding layer must never crash on user input. Interviewers use this to evaluate careful single-pass string scanning, clearly stated grammar rules, and graceful handling of malformed queries.

What the Interviewer Expects
  • States the grammar before coding, including what a lone minus or trailing colon means
  • Scans with an index instead of naive split so quoted spaces survive
  • Normalizes whitespace inside phrases and lowercases terms
  • Handles unclosed quotes without throwing
  • Collects repeated filters as lists
Key Topics to Cover
string parsing
tokenization
state machines
query understanding
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
  • How would you support escaped quotes inside a phrase?
  • How would you validate filter keys against an allowed set and value types like price ranges?
  • What would you change for languages without spaces, such as Japanese?
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
  • Whitespace separates tokens except inside double quotes.
  • "..." is a phrase; -"..." is an excluded phrase.
  • -word (minus directly followed by text) is an exclusion. A minus followed by a space is ...
High-Level Design

A single left-to-right scan with an index. At each non-space position, check for a leading minus, then either read a quoted phrase up to the next quote or read a bare token up to whitespace. Classify ...


Submit Your Answer
Markdown supported