← Harvey AI Interview Insights

Harvey AI·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
May 2026

Summary

Got a coding question at Harvey AI for a software engineer role that was basically about string matching and annotation. Three parts, each building on the last, and the third part is where things got interesting.

Questions Asked (1)

Q1

Given an LLM output string and a list of source strings, design an algorithm to count how many times each source string appears in the LLM output. Then extend it to wrap every occurrence of a source in XML-style tags, handling overlapping matches correctly. Finally, modify the tagging so each tag includes annotations indicating which source indices matched at that position.

Algorithms & Data StructuresTechnical Trade-offsSystem Design
Author's notes

The first part is basically substring frequency counting, fine.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying requirements: case sensitivity, word boundaries, and whether matches can overlap. Then propose a solution using a multi-pattern matching algorithm like Aho-Corasick for efficiency, and for tagging, collect all match intervals, merge overlaps, and insert tags with source indices. Discuss trade-offs between time/space complexity and simplicity.

Pro tip: Mention that overlapping matches require careful interval merging to avoid nested or broken tags, and that using a trie-based approach like Aho-Corasick is ideal for multiple patterns. Also, consider edge cases like empty strings or matches at the same position.

1. Clarify requirements and constraints

Ask about case sensitivity, word boundaries, overlapping matches, and expected input sizes. This ensures the algorithm meets the actual needs and avoids over-engineering.

2. Design counting algorithm

Propose using Aho-Corasick for O(n + m + k) time where n is output length, m is total pattern length, and k is number of matches. Alternatively, for small inputs, a naive approach with string search per source is acceptable.

3. Extend to tagging with overlap handling

Collect all match intervals (start, end, source_index). Sort by start, then merge overlapping intervals, combining source indices for intervals that overlap. Insert opening tags before the interval and closing tags after.

4. Annotate tags with source indices

For each merged interval, include the set of source indices that matched. Format tags as <match sources='1,3'>...</match> or similar, ensuring indices are unique and sorted.

5. Analyze complexity and trade-offs

Discuss time and space complexity of the chosen approach, compare with alternatives (e.g., regex, naive), and mention potential optimizations or limitations.

Key Points to Mention

  • Aho-Corasick algorithm for efficient multi-pattern matching
  • Handling overlapping matches by merging intervals and combining source indices
  • Time and space complexity analysis (e.g., O(n + m + k) for Aho-Corasick)
  • Edge cases: empty strings, matches at same position, case sensitivity
  • Trade-offs between simplicity (naive) and efficiency (Aho-Corasick) based on input size
  • XML tag formatting and escaping special characters in output

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