Wrap Matched Keywords in Bold Tags for Search Snippets

Last updated: November 29, 2026

Quick Overview

Given a snippet string and a list of keywords, insert <b> and </b> tags so that every occurrence of every keyword is bold, merging overlapping or touching bold regions into one tag pair.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Given a snippet string and a list of keywords, insert <b> and </b> tags so that every occurrence of every keyword is bold, merging overlapping or touching bold regions into one tag pair.

A Google technical screen question taken straight from how Search result snippets highlight query terms. The interviewer checks that you find all matches efficiently with a trie, mark a boolean coverage array, and emit the minimal tag set.

What the Interviewer Expects
  • Find all keyword occurrences, including overlaps
  • Use a trie so each start index walks only as far as the longest keyword match
  • Mark covered characters, then emit tags at run boundaries
  • Merge adjacent runs, not just overlapping ones
  • Analyze cost versus calling find for every keyword
Key Topics to Cover
trie
interval merging
string building
highlighting
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 make matching case-insensitive while keeping the original casing?
  • How would you handle HTML entities already present in the snippet?
  • How would Aho-Corasick change the time bound?
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: string s and list words. Output: s with <b>/</b> inserted so that exactly the characters covered by some keyword occurrence are inside bold tags, using as few tag pairs as possible. `...

High-Level Design

Two phases. First compute a boolean array bold of length n: for each start i, walk a trie of keywords along s[i:] and remember the farthest end of any keyword that matched; mark bold[i..end]...


Submit Your Answer
Markdown supported