Preparing your design workspace

Loading the problem and your latest saved attempt…

Design Search Result Highlighting

Design a low-latency service that returns safe, version-consistent snippets and highlighted match segments for ranked search results.

Problem statement

For this exercise, assume an upstream search service already returns authorized, ranked document IDs and the query terms that matched each result.

Design the highlighting path for text documents: each visible result should include a short snippet with every displayed match marked, without corrupting Unicode text or allowing document content to become executable markup. Highlights must correspond to the exact document version represented by the result.

Candidate retrieval, ranking, permission evaluation, document editing, and the final browser styling are outside scope.

What to build

  1. For each ranked result, return a compact plain-text snippet split into highlighted and non-highlighted segments.
  2. Represent repeated, overlapping, and multi-term matches deterministically without duplicating or dropping displayed text.
  3. Generate highlights from the same immutable document version named by the search result, or return an explicit safe fallback when that version is unavailable.
  4. Keep document text inert so a client can render highlights without interpreting stored text as HTML or script.

At scale

  • For this exercise, assume 50,000 highlight requests per second at peak, with highly skewed repeated queries and popular documents.
  • For this exercise, assume one response contains at most 20 results, a document may contain 2 MB of UTF-8 text, and the highlighting path should finish within 100 ms at p95.
  • For this exercise, assume indexed document versions may lag edits by up to five minutes; a wrong-version highlight is forbidden, while an explicitly unhighlighted summary is an acceptable degraded result.
  • For this exercise, assume each displayed snippet is at most 300 extended grapheme clusters and truncation must preserve complete displayed characters.