← Harvey Interview Insights

Harvey·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
May 2026

Summary

Got a coding question at Harvey for a software engineer role that was basically a string manipulation problem with a merging twist. Not the hardest thing I've seen but the follow-ups added some real complexity.

Questions Asked (3)

Q1

Given a string and a list of source strings, find every exact (case-sensitive) occurrence of any source string in the main text, wrap matched characters with highlight tags, and merge any overlapping or adjacent matched ranges into a single highlighted region before returning the final string.

Algorithms & Data Structures
Author's notes

The merging part is what gets you.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify the requirements and edge cases, then propose an efficient algorithm that finds all matches, merges overlapping/adjacent intervals, and constructs the final string. Walk through a small example to validate the approach, and discuss time/space complexity.

Pro tip: Mention that you would use a multi-pattern matching algorithm like Aho-Corasick for efficiency, but also note that a simpler approach with sorting and merging intervals works well for most cases. This shows you understand trade-offs.

1. Clarify requirements and edge cases

Ask about input constraints (string lengths, number of source strings), case sensitivity, overlapping matches, and whether source strings can be empty. Confirm that matches should be merged if they overlap or are adjacent.

2. Find all matches

Iterate over each source string and find all its occurrences in the main text. Record each match as a pair of start and end indices. Consider using a multi-pattern search algorithm for efficiency.

3. Merge overlapping and adjacent intervals

Sort the intervals by start index. Then iterate through them, merging any interval that overlaps or is adjacent to the previous merged interval. This yields a list of non-overlapping, non-adjacent intervals.

4. Construct the final string

Build the result by appending the text before each merged interval, then the highlighted substring (wrapped with tags), and finally the remaining text after the last interval.

5. Analyze complexity and test

Discuss time and space complexity. For the naive approach, it's O(n * m * k) where n is text length, m is number of source strings, and k is average source length. With Aho-Corasick, it's O(n + total pattern length + number of matches). Walk through a test case to verify correctness.

Key Points to Mention

  • Case-sensitive exact matching
  • Handling overlapping and adjacent matches by merging intervals
  • Efficient multi-pattern search (e.g., Aho-Corasick) vs. naive approach
  • Sorting and merging intervals algorithm
  • String construction with highlight tags
  • Time and space complexity analysis

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q2

As a follow-up: for each source string, count how many exact occurrences it has in the original text.

Algorithms & Data Structures
Author's notes

Pretty standard once the main problem is done.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify whether occurrences can overlap and whether matches must be case-sensitive or whole-word. Then propose an efficient algorithm such as building a suffix automaton or Aho-Corasick automaton over the original text to answer multiple pattern queries in linear time. For each source string, traverse the automaton and return the count of occurrences.

Pro tip: Mention that if the number of source strings is small, a simpler approach like KMP per pattern may be acceptable, but for many patterns, Aho-Corasick is the scalable choice. Also, discuss how to handle overlapping occurrences (e.g., 'aaa' in 'aaaa' has 2 occurrences) and confirm the definition with the interviewer.

1. Clarify requirements

Ask whether occurrences can overlap, whether matching is case-sensitive, and if whole-word matching is required. Confirm the expected input size and performance constraints.

2. Choose data structure

Select an appropriate algorithm based on the number of source strings and text length. For many patterns, use Aho-Corasick; for a single pattern, KMP or suffix automaton; for few patterns, a simple scan may suffice.

3. Preprocess the text

Build the chosen automaton (e.g., Aho-Corasick trie with failure links) or suffix automaton from the original text. This preprocessing enables efficient querying.

4. Count occurrences per pattern

For each source string, traverse the automaton to find all matches, incrementing a counter for each occurrence. Ensure overlapping matches are counted if allowed.

5. Analyze complexity and edge cases

State the time and space complexity (e.g., O(|text| + total pattern length + number of matches) for Aho-Corasick). Discuss edge cases like empty patterns, patterns longer than text, and special characters.

Key Points to Mention

  • Overlapping occurrences: clarify if they should be counted (e.g., 'aa' in 'aaa' appears twice).
  • Algorithm choices: Aho-Corasick for multiple patterns, KMP for single pattern, suffix automaton for repeated queries.
  • Time complexity: preprocessing O(|text| + sum of pattern lengths), querying O(|pattern| + occurrences).
  • Space complexity: O(|text| * alphabet size) for automaton, or O(|text|) for suffix automaton.
  • Edge cases: empty source string, pattern not found, pattern longer than text, case sensitivity.
  • Implementation details: building failure links, handling character encoding, and using a trie for efficient traversal.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q3

Second follow-up: for each merged highlighted region in the output, return which source strings contributed at least one match inside that region.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This one required more bookkeeping than I expected.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify that each merged region is a union of overlapping intervals from multiple source strings, and we need to map each region back to the set of sources that contributed at least one match within it. Propose maintaining a mapping from each match to its source, then during the merge process, propagate source IDs to the merged region. Finally, output each region with the deduplicated set of source IDs.

Pro tip: Mention that if the number of sources is large, using bitsets or bloom filters can efficiently represent and merge source sets, but for typical interview constraints a simple set union is sufficient. Also, emphasize that the merge step should be stable and preserve source attribution.

1. Clarify the data model

Confirm that each match is associated with a source string (e.g., via an ID) and that merged regions are formed by overlapping matches from any sources. Ensure you understand what 'contributed at least one match' means: any match from that source that falls within the region's boundaries.

2. Design the merge algorithm with source tracking

Sort all matches by start position. Iterate through them, merging overlapping intervals. For each merged region, maintain a set of source IDs from all matches that have been merged into it.

3. Handle edge cases and efficiency

Consider cases where a source has multiple matches in the same region (deduplicate), and where matches are adjacent but not overlapping (decide if they merge). Discuss time complexity: O(n log n) for sorting plus O(n) for merging, with set operations adding overhead.

4. Produce the output

For each merged region, output its start and end positions along with the list of source IDs that contributed. Ensure the source IDs are unique and sorted for consistency.

5. Test with examples

Walk through a small example with multiple sources and overlapping matches to verify the logic, including cases where a source's match is completely contained within another's.

Key Points to Mention

  • Associating each match with its source ID before merging.
  • Using a set (or bitset) to accumulate source IDs per merged region.
  • Sorting matches by start position to enable efficient merging.
  • Handling overlapping and adjacent intervals correctly.
  • Deduplicating source IDs in the final output.
  • Time and space complexity analysis, including the cost of set operations.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.