Highlight Overlapping Phrases in a Sentence

Quick Overview

Given a sentence such as `The quick brown fox jumps over the lazy dog.` and phrases such as `quick brown` and `brown fox jumps`, design a function that returns the sentence with every character covered by any exact phrase match highlighted. Make the API or object boundaries explicit, then cover invariants, edge cases, testing strategy, and operational trade-offs.

Highlight Overlapping Phrases in a Sentence

Company: Harvey

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: hard

Interview Round: Technical Screen

# Highlight Overlapping Phrases in a Sentence Given a sentence such as `The quick brown fox jumps over the lazy dog.` and phrases such as `quick brown` and `brown fox jumps`, design a function that returns the sentence with every character covered by any exact phrase match highlighted. The rendering markers are configurable; focus on identifying and merging covered ranges so overlaps produce one continuous highlighted region. ### Constraints & Assumptions - Matching is case-sensitive and phrases may overlap. - The original sentence, spacing, and punctuation must be preserved. - Empty phrases are ignored. ### Clarifying Questions to Ask - Should every occurrence of each phrase be highlighted? - Can one match begin inside another, and should adjacent ranges merge? ```hint Protect the original text Test a phrase inside another phrase, phrases that only touch, and a repeated phrase; every source character must appear exactly once in the rendered output. ``` ### What a Strong Answer Covers - Correct discovery of all occurrences, including overlaps. - Stable interval sorting and merging without changing the source text. - Complexity trade-offs between repeated search and multi-pattern matching. ### Follow-up Questions - How would you handle millions of phrases? - How would case-insensitive Unicode matching affect offsets?

Quick Answer: Given a sentence such as `The quick brown fox jumps over the lazy dog.` and phrases such as `quick brown` and `brown fox jumps`, design a function that returns the sentence with every character covered by any exact phrase match highlighted. Make the API or object boundaries explicit, then cover invariants, edge cases, testing strategy, and operational trade-offs.

|Home/Software Engineering Fundamentals/Harvey
Harvey logo
Harvey
Jul 26, 2026, 12:00 AM
hardSoftware EngineerTechnical ScreenSoftware Engineering Fundamentals
1
0

Highlight Overlapping Phrases in a Sentence

Given a sentence such as The quick brown fox jumps over the lazy dog. and phrases such as quick brown and brown fox jumps, design a function that returns the sentence with every character covered by any exact phrase match highlighted. The rendering markers are configurable; focus on identifying and merging covered ranges so overlaps produce one continuous highlighted region.

Constraints & Assumptions

  • Matching is case-sensitive and phrases may overlap.
  • The original sentence, spacing, and punctuation must be preserved.
  • Empty phrases are ignored.

Clarifying Questions to Ask Guidance

  • Should every occurrence of each phrase be highlighted?
  • Can one match begin inside another, and should adjacent ranges merge?

What a Strong Answer Covers Guidance

  • Correct discovery of all occurrences, including overlaps.
  • Stable interval sorting and merging without changing the source text.
  • Complexity trade-offs between repeated search and multi-pattern matching.

Follow-up Questions Guidance

  • How would you handle millions of phrases?
  • How would case-insensitive Unicode matching affect offsets?
Loading comments...