Find longest duplicated substring
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This question evaluates proficiency in string algorithms, pattern matching, and algorithmic complexity analysis, focusing on designing efficient data structures and algorithms for finding repeated substrings.
Constraints
- 1 <= len(s) <= 2 * 10^5
- s consists only of lowercase English letters
- Overlapping occurrences are allowed
Examples
Input: ('banana',)
Expected Output: 'ana'
Explanation: 'ana' appears twice: at indices 1..3 and 3..5. No duplicated substring of length 4 exists.
Input: ('abcd',)
Expected Output: ''
Explanation: Every non-empty substring appears only once.
Input: ('aaaaa',)
Expected Output: 'aaaa'
Explanation: 'aaaa' appears twice with overlapping occurrences: indices 0..3 and 1..4.
Input: ('z',)
Expected Output: ''
Explanation: A single-character string has no substring that appears at least twice.
Input: ('mississippi',)
Expected Output: 'issi'
Explanation: 'issi' appears at indices 1..4 and 4..7, and no longer duplicated substring exists.
Input: ('abcabxabcd',)
Expected Output: 'abc'
Explanation: 'abc' appears at indices 0..2 and 6..8. No duplicated substring of length 4 exists.
Hints
- All duplicated substrings can be represented compactly using a suffix-based structure such as a suffix array or suffix automaton.
- In a suffix automaton, each state represents substrings with the same set of ending positions. If a state is reached by at least two end positions, the longest substring represented by that state is duplicated.