Highlight Exact Source Matches
Company: Harvey
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates string-processing and algorithmic competencies, specifically exact substring matching, interval merging for overlapping or adjacent matches, and robust text transformation to produce correctly tagged output.
Part 1: Wrap Exact Source Matches in <yellow> Tags
Constraints
- 0 <= len(text) <= 2000
- 0 <= len(sources) <= 100
- 0 <= len(source) <= 50 for each source in sources
- Matching is exact substring matching and is case-sensitive
- Overlapping or adjacent matches must be merged into one highlighted region
Examples
Input: ("abcxyz123", ["abc", "xyz", "z12"])
Expected Output: "<yellow>abcxyz12</yellow>3"
Explanation: Matches are [0,3), [3,6), and [5,8). They merge into [0,8).
Input: ("foobarbaz", ["foo", "baz"])
Expected Output: "<yellow>foo</yellow>bar<yellow>baz</yellow>"
Explanation: There are two separate highlighted regions.
Input: ("aaab", ["aa", "aab"])
Expected Output: "<yellow>aaab</yellow>"