Bold Query Term Matches in a Search Snippet
Last updated: November 29, 2026
Quick Overview
Given a snippet of document text and a list of query terms, return the snippet with every whole-word, case-insensitive match wrapped in <b> tags, merging consecutive matches separated only by whitespace into a single bold run.
Glean
November 29, 20260
0
0 solved
Given a snippet of document text and a list of query terms, return the snippet with every whole-word, case-insensitive match wrapped in <b> tags, merging consecutive matches separated only by whitespace into a single bold run.
Every Glean search result shows a snippet with the query terms highlighted, so this is a natural warm-up on a search team phone screen. The interviewer is checking clean tokenization, whole-word matching instead of substring matching, and careful handling of tag boundaries so output HTML stays well formed.
What the Interviewer Expects
- Tokenizes into word and non-word runs so punctuation is preserved exactly
- Matches whole words case-insensitively, not substrings (plan should not bold planning)
- Merges adjacent matches separated by a single whitespace run into one <b> span
- Always closes an open tag at the end of the text
- States O(n) time in snippet length
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
- How would you also bold stemmed variants such as roadmaps for the query roadmap?
- The snippet will be rendered as HTML. What must you escape and in what order?
- How would you highlight a quoted phrase only when its words appear in sequence?
- How would you choose which 200 characters of a long document to show in the first place?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: text (a snippet already chosen from a document) and terms (query words). Output: the same text with matches wrapped in <b>...</b>. A match is a whole word equal to a query term ignoring c...
High-Level Design
Split the text with \w+|\W+ so every character belongs to exactly one token and joining tokens reproduces the original. Walk the tokens with a flag open_tag. A matching word opens a tag if one is ...