Longest Repeated Snippet in a Source File Using Rolling Hashes
Last updated: November 29, 2026
Quick Overview
Given a source file as a string, return the longest substring that occurs at least twice (occurrences may overlap), or an empty string if no character repeats.
0
0
0 solved
Given a source file as a string, return the longest substring that occurs at least twice (occurrences may overlap), or an empty string if no character repeats.
A Google onsite question themed on code search and copy-paste detection across a large monorepo. The interviewer expects binary search on the answer length combined with Rabin-Karp hashing, and will ask how you guard against hash collisions.
What the Interviewer Expects
- Observes monotonicity: if length L repeats, so does every shorter length
- Binary searches the length
- Uses a rolling polynomial hash for O(n) per length check
- Verifies candidate matches to rule out collisions
- States O(n log n) expected time and mentions suffix arrays
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you find duplicates across many files at once?
- Explain how a suffix array plus LCP array solves this deterministically.
- Find the longest substring that repeats at least k times.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: string
s, length up to about 3 * 10^4 (or larger). - Output: any longest substring that appears at least twice; overlaps allowed.
- Return
''if none exists....
High-Level Design
If some substring of length L appears twice, then its prefix of length L - 1 also appears twice, so the predicate 'a repeat of length L exists' is monotone and we can binary search L in [0, n-1]. For ...