Design search result highlighting
I was asked this
No reports yet. Sign in to say where you were asked it.
A system design interview question asked in Figma's system design round, last asked Jan 2021. It tests search, text processing and caching.
A search service already returns, for each query, a ranked list of document IDs the user may see and the query terms each one matched. Design the step that adds a short snippet to every visible result, with each displayed match marked.
The snippet must come from the exact document version the result refers to. It has to handle repeated and overlapping matches, never split a Unicode character, and never let document text turn into executable markup in the client. Retrieval, ranking, permission checks, editing and the final styling in the browser are out of scope.
Requirements
- Functional: for each result, return a compact plain-text snippet split into segments, each marked highlighted or not; handle several terms, repeated matches and overlapping matches the same way every time, without repeating or dropping text; build the snippet from the document version named in the result, or return an explicit safe fallback when that version is not available.
- Safety: the client must be able to render the snippet without interpreting stored text as HTML or script.
- Non-functional: snippets for a page of results must not add much to search latency; documents can be large.
Clarifying questions
- Does the search result carry a document version, and are versions immutable?
- Are matches exact terms only, or also stems, prefixes and synonyms? Does the search service give match offsets, or only terms?
- How long should a snippet be, and how many snippets per result?
- What languages and scripts must work (word boundaries in CJK text, combining characters, emoji)?
- What should the fallback show when the version is missing: the title only, or a snippet from the latest version with a flag?
What a strong answer covers
- The response shape: an array of
{text, highlighted}segments, never an HTML string, so the client inserts text nodes and the markup problem disappears. - Choosing the window: score candidate windows by how many distinct terms they cover, and cut on word or grapheme boundaries.
- Merging overlapping and adjacent match ranges into one highlighted segment, with offsets in one unit (code points or UTF-8 bytes) used consistently end to end.
- Where text comes from: a store keyed by document ID and version, or match offsets and snippets stored with the index; the trade-off between index size and fetch latency.
- Caching snippets by document version and query terms, and a per-request budget with parallel fetches.
- The version-mismatch fallback, and tests for Unicode edge cases and for text that looks like markup.
Follow-ups
- The index stores offsets in UTF-8 bytes but the client counts UTF-16 units. Where do you convert?
- Support phrase queries, so only whole-phrase matches are marked.
- How do you keep snippet latency flat when one result is a 50 MB document?
Where it's asked
- Figma · System design · Last asked Jan 2021Design discussion; one or two rounds
What it tests
No official answer yet. Draw yours on the right; publish it and it shows under Community.
Nothing published yet. Publish your canvas and it shows here for others to open and remix.
