Shortest Review Snippet Containing All Search Terms

Last updated: November 29, 2026

Quick Overview

Given the text of a review and a list of search terms (which may repeat), return the shortest contiguous run of words that contains every term at least as many times as it appears in the list, or an empty string if none exists.

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

0

0

0 solved


Given the text of a review and a list of search terms (which may repeat), return the shortest contiguous run of words that contains every term at least as many times as it appears in the list, or an empty string if none exists.

When someone searches Yelp for "spicy ramen", the result card highlights a short review excerpt that mentions those words. This question checks the classic variable size sliding window with counts, plus tokenization decisions that matter for real review text.

What the Interviewer Expects
  • Clarify tokenization: case, punctuation, apostrophes
  • Use a sliding window with a need counter and a missing count
  • Shrink from the left while the window is valid and record the best
  • Handle duplicate terms in the query correctly
  • Achieve O(number of words) time
Key Topics to Cover
sliding window
two pointers
hash counting
tokenization
search snippets
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 prefer snippets that end at sentence boundaries?
  • How would you treat stemming so that tacos matches taco?
  • If you can return a snippet up to 30 words, how do you pick the one with the most distinct terms?
  • How would you precompute this across millions of reviews at index time?
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

Tokenize to lowercase words of letters, digits and apostrophes. Terms are compared case insensitively. If the query is ["a", "a"], the window must contain a twice. Return the window joined by spac...

High-Level Design

Keep need, a counter of required terms, and have, the counts inside the window. missing is the number of distinct terms whose requirement is not met yet. Extend right one word at a time. When ...


Submit Your Answer
Markdown supported

Related Questions