Count, Tag and Cite Source Phrases in a Text Passage With Merged Overlaps

Quick Overview

A three-part coding exercise on finding source phrases in a text passage: count how often each source occurs, wrap matched regions in tags while merging overlapping matches, and append citations naming the matching sources. It tests string search, interval merging, whole-word boundary checks and writing your own tests.

Count, Tag and Cite Source Phrases in a Text Passage With Merged Overlaps

Company: Harvey

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Technical Screen

You are given a passage of text and a list of sources. Each source is a string, such as a phrase quoted from a reference document, that may appear in the passage. Write code that finds where the sources occur and marks them up. The exercise had three parts, and the code was run against a few tests at the end. The report states Part 1 directly. For the last two parts it says only that they add tags and citations to the matched text, that overlapping matches were combined by merging intervals, and that word boundaries needed care. The split between Parts 2 and 3 and the exact markup below are assumptions to confirm with the interviewer. ### Clarifying Questions - Is matching case-sensitive, and must spaces and punctuation inside a source match exactly? - If two occurrences of the same source overlap (possible when a source starts and ends with the same word, as `very very` does in `very very very`), does each one count? - Can the list contain duplicates or empty strings, and can one source contain another, such as `the statute` and `statute`? - Is a source only a string, or a record with an identifier that the citation should show? - How long can the passage be, and how many sources can there be? This decides whether searching once per source is fast enough. ### Part 1: Count each source's occurrences Return how many times each source occurs in the passage, including sources that do not occur at all. ```python def count_occurrences(text: str, sources: list[str]) -> dict[str, int]: ``` For example: ```python text = "the court held that the statute applies; the statute was later amended" sources = ["the statute", "court held", "amended", "appeal"] count_occurrences(text, sources) # {"the statute": 2, "court held": 1, "amended": 1, "appeal": 0} ``` ```hint Find every start One `find` call returns only the first position. Decide where the next search must begin so that you neither skip an occurrence nor count one twice. ``` #### What This Part Should Cover - Finding every occurrence, and reporting zero for a source that never appears - An explicit choice about overlapping occurrences and case - Cost in terms of passage length, number of sources and source length ### Part 2: Tag the matched text Return the passage with every matched region wrapped in a tag. Assume the opening tag is `<cite>` and the closing tag is `</cite>`. Matches from different sources can overlap or contain one another, but tags must never nest or interleave: overlapping matches, and matches that touch (one ends exactly where the next begins), form a single tagged region. Regions separated by at least one character, even a single space, stay separate. ```python def tag_matches(text: str, sources: list[str]) -> str: ``` For example: ```python text = "the court held that the statute applies" sources = ["court held that", "that the statute", "applies"] tag_matches(text, sources) # "the <cite>court held that the statute</cite> <cite>applies</cite>" ``` The first two sources overlap on the word `that`, so they form one region. The match for `applies` is separated from it by a space and gets its own tags. ```hint Record before you rewrite Inserting tags while you search shifts every later position in the string. Think about what to record for each match first, and when to build the output. ``` #### What This Part Should Cover - Representing matches as index ranges and combining overlapping, nested and touching ones - Building the output in one pass without positions drifting - Correct output when matches chain together or sit at the very start or end of the passage ### Part 3: Cite the sources and match whole words only Make two changes. 1. A source now matches only as whole words. The character just before a match must be the start of the passage or a non-word character, and the character just after it must be the end of the passage or a non-word character, where word characters are letters and digits. The same rule now applies to the counts from Part 1. 2. After each closing tag, append a citation for every source that has a whole-word match inside that region, written as `[n]`, where `n` is the source's 1-based position in `sources`. Cite each source once per region, in ascending order of `n`. ```python def tag_and_cite(text: str, sources: list[str]) -> str: ``` For example: ```python text = "the art party cited the art statute" sources = ["art", "the art statute", "party"] tag_and_cite(text, sources) # "the <cite>art</cite>[1] <cite>party</cite>[3] cited <cite>the art statute</cite>[1][2]" ``` The letters `art` inside `party` do not match, because `p` comes right before them. The last region cites sources 1 and 2, because the match for `art` lies inside the match for `the art statute`. ```hint Stress the boundary check Run your boundary check on a source that begins or ends with punctuation, such as `(a)` or `Inc.`, and on a match that ends at the very last character of the passage. ``` #### Clarifying Questions for this Part - Should the citation go inside the tag, for example as an attribute, instead of after it? - Should an apostrophe or a hyphen count as part of a word, so that `court` does not match inside `court's` or `well-known`? #### What This Part Should Cover - A boundary check that also works for sources that start or end with punctuation - Tracking which sources fall inside each combined region and printing their citations in a fixed order - One shared matcher, so counts, tags and citations always agree ### What a Strong Answer Covers - Running code for all three parts, checked with tests the candidate writes, including the examples above - Finding matches kept separate from rendering, so later parts reuse earlier code - Interval merging that is correct for overlap, containment and touching ranges - Edge cases: empty or repeated sources, no matches, matches at the edges of the passage, sources contained in other sources - Stated complexity, and a faster way to match when there are many sources ### Follow-up Questions - With thousands of sources and long documents, how would you find all matches without scanning the passage once per source? - The passage already contains markup. How do you avoid matching inside existing tags or producing broken markup? - How would you support case-insensitive matching while keeping the passage's original text in the output? - A front end wants to render the highlights itself. What would you return instead of a marked-up string?

Overview: A three-part coding exercise on finding source phrases in a text passage: count how often each source occurs, wrap matched regions in tags while merging overlapping matches, and append citations naming the matching sources. It tests string search, interval merging, whole-word boundary checks and writing your own tests.

Harvey logo
Harvey
Nov 5, 2026
mediumSoftware EngineerTechnical ScreenSoftware Engineering Fundamentals
2
0

You are given a passage of text and a list of sources. Each source is a string, such as a phrase quoted from a reference document, that may appear in the passage. Write code that finds where the sources occur and marks them up. The exercise had three parts, and the code was run against a few tests at the end.

The report states Part 1 directly. For the last two parts it says only that they add tags and citations to the matched text, that overlapping matches were combined by merging intervals, and that word boundaries needed care. The split between Parts 2 and 3 and the exact markup below are assumptions to confirm with the interviewer.

Clarifying Questions Guidance

  • Is matching case-sensitive, and must spaces and punctuation inside a source match exactly?
  • If two occurrences of the same source overlap (possible when a source starts and ends with the same word, as very very does in very very very ), does each one count?
  • Can the list contain duplicates or empty strings, and can one source contain another, such as the statute and statute ?
  • Is a source only a string, or a record with an identifier that the citation should show?
  • How long can the passage be, and how many sources can there be? This decides whether searching once per source is fast enough.

Part 1: Count each source's occurrences

Return how many times each source occurs in the passage, including sources that do not occur at all.

def count_occurrences(text: str, sources: list[str]) -> dict[str, int]:

For example:

text = "the court held that the statute applies; the statute was later amended"
sources = ["the statute", "court held", "amended", "appeal"]
count_occurrences(text, sources)
# {"the statute": 2, "court held": 1, "amended": 1, "appeal": 0}

What This Part Should Cover Guidance

  • Finding every occurrence, and reporting zero for a source that never appears
  • An explicit choice about overlapping occurrences and case
  • Cost in terms of passage length, number of sources and source length

Part 2: Tag the matched text

Return the passage with every matched region wrapped in a tag. Assume the opening tag is <cite> and the closing tag is </cite>. Matches from different sources can overlap or contain one another, but tags must never nest or interleave: overlapping matches, and matches that touch (one ends exactly where the next begins), form a single tagged region. Regions separated by at least one character, even a single space, stay separate.

def tag_matches(text: str, sources: list[str]) -> str:

For example:

text = "the court held that the statute applies"
sources = ["court held that", "that the statute", "applies"]
tag_matches(text, sources)
# "the <cite>court held that the statute</cite> <cite>applies</cite>"

The first two sources overlap on the word that, so they form one region. The match for applies is separated from it by a space and gets its own tags.

What This Part Should Cover Guidance

  • Representing matches as index ranges and combining overlapping, nested and touching ones
  • Building the output in one pass without positions drifting
  • Correct output when matches chain together or sit at the very start or end of the passage

Part 3: Cite the sources and match whole words only

Make two changes.

  1. A source now matches only as whole words. The character just before a match must be the start of the passage or a non-word character, and the character just after it must be the end of the passage or a non-word character, where word characters are letters and digits. The same rule now applies to the counts from Part 1.
  2. After each closing tag, append a citation for every source that has a whole-word match inside that region, written as [n] , where n is the source's 1-based position in sources . Cite each source once per region, in ascending order of n .
def tag_and_cite(text: str, sources: list[str]) -> str:

For example:

text = "the art party cited the art statute"
sources = ["art", "the art statute", "party"]
tag_and_cite(text, sources)
# "the <cite>art</cite>[1] <cite>party</cite>[3] cited <cite>the art statute</cite>[1][2]"

The letters art inside party do not match, because p comes right before them. The last region cites sources 1 and 2, because the match for art lies inside the match for the art statute.

Clarifying Questions for this Part Guidance

  • Should the citation go inside the tag, for example as an attribute, instead of after it?
  • Should an apostrophe or a hyphen count as part of a word, so that court does not match inside court's or well-known ?

What This Part Should Cover Guidance

  • A boundary check that also works for sources that start or end with punctuation
  • Tracking which sources fall inside each combined region and printing their citations in a fixed order
  • One shared matcher, so counts, tags and citations always agree

What a Strong Answer Covers Guidance

  • Running code for all three parts, checked with tests the candidate writes, including the examples above
  • Finding matches kept separate from rendering, so later parts reuse earlier code
  • Interval merging that is correct for overlap, containment and touching ranges
  • Edge cases: empty or repeated sources, no matches, matches at the edges of the passage, sources contained in other sources
  • Stated complexity, and a faster way to match when there are many sources

Follow-up Questions Guidance

  • With thousands of sources and long documents, how would you find all matches without scanning the passage once per source?
  • The passage already contains markup. How do you avoid matching inside existing tags or producing broken markup?
  • How would you support case-insensitive matching while keeping the passage's original text in the output?
  • A front end wants to render the highlights itself. What would you return instead of a marked-up string?