PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

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.

  • hard
  • Google
  • Coding & Algorithms
  • Software Engineer

Find longest duplicated substring

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

You are given a string `s` consisting of lowercase English letters. A **substring** of `s` is any contiguous sequence of characters, i.e., `s[i..j)` for `0 ≤ i < j ≤ len(s)`. Design an algorithm to find **any one** of the longest substrings that appears **at least twice** in `s`. The two occurrences may overlap. If no substring appears at least twice, return the empty string. ### Requirements - Input: a string `s` of length `n` (e.g., `1 ≤ n ≤ 2 × 10^5`). - Output: a string representing one of the longest duplicated substrings. - Aim for an algorithm substantially better than the naive `O(n^2)` substring comparison approach. ### Examples - `s = "banana"` - Possible duplicated substrings: "a", "an", "ana" - Longest length is 3 ("ana"), so acceptable outputs include `"ana"`. - `s = "abcd"` - No duplicated substring, so output should be `""` (empty string). Describe your algorithm, its time and space complexity, and how it scales for large `n`.

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.

You are given a string s consisting of lowercase English letters. A substring is any contiguous non-empty sequence of characters in s. Return any one of the longest substrings that appears at least twice in s. The two occurrences may overlap. If no non-empty substring appears at least twice, return the empty string. Your algorithm should be substantially better than comparing all pairs of substrings naively.

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

  1. All duplicated substrings can be represented compactly using a suffix-based structure such as a suffix array or suffix automaton.
  2. 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.
Last updated: Jul 9, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Find A Threshold-Limited Path With Minimum Required Safety - Google (medium)
  • Build Prefix Lookup with a Trie - Google (medium)
  • Deterministic Task Execution Order - Google (easy)
  • Busiest Rental Car - Google (easy)
  • Find Common Free Time Slots Across Calendars - Google (easy)