Find the length of the longest substring that occurs at least twice

Quick Overview

Given a string of lowercase letters, return the length of the longest substring that appears at least twice, where the two occurrences may overlap. Tests binary search on an answer length, rolling hashes that handle collisions correctly, and a quadratic dynamic programming check.

Find the length of the longest substring that occurs at least twice

Company: Coupang

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given a string `s`, return the length of the longest substring that occurs at least twice in `s`. If no such repeating substring exists, return `0`. The two occurrences are allowed to overlap. ### Function Signature ```python def longest_repeating_substring(s: str) -> int: ``` ### Rules - A substring is a contiguous block of characters of `s`. Two occurrences count as different when they start at different indices. They may overlap, so in `"aaa"` the substring `"aa"` occurs twice (at indices 0 and 1). - Return only the length, as an integer. Return `0` when no substring of length 1 or more appears twice. ### Constraints - `2 <= len(s) <= 2000` - `s` consists of lowercase English letters. ### Examples ```text Input: s = "abcd" Output: 0 ``` All four letters are different, so not even a single character repeats. ```text Input: s = "abbaba" Output: 2 ``` `"ab"` occurs at indices 0 and 3, and `"ba"` occurs at indices 2 and 4. The four substrings of length 3 (`"abb"`, `"bba"`, `"bab"`, `"aba"`) are all different, so no length-3 substring repeats. ```text Input: s = "aaaaa" Output: 4 ``` `"aaaa"` starts at index 0 and at index 1. The two occurrences overlap, which is allowed. The whole string of length 5 occurs only once.

Overview: Given a string of lowercase letters, return the length of the longest substring that appears at least twice, where the two occurrences may overlap. Tests binary search on an answer length, rolling hashes that handle collisions correctly, and a quadratic dynamic programming check.

Coupang logo
Coupang
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given a string s, return the length of the longest substring that occurs at least twice in s.

If no such repeating substring exists, return 0. The two occurrences are allowed to overlap.

Function Signature

def longest_repeating_substring(s: str) -> int:

Rules

  • A substring is a contiguous block of characters of s . Two occurrences count as different when they start at different indices. They may overlap, so in "aaa" the substring "aa" occurs twice (at indices 0 and 1).
  • Return only the length, as an integer. Return 0 when no substring of length 1 or more appears twice.

Constraints

  • 2 <= len(s) <= 2000
  • s consists of lowercase English letters.

Examples

Input: s = "abcd"
Output: 0

All four letters are different, so not even a single character repeats.

Input: s = "abbaba"
Output: 2

"ab" occurs at indices 0 and 3, and "ba" occurs at indices 2 and 4. The four substrings of length 3 ("abb", "bba", "bab", "aba") are all different, so no length-3 substring repeats.

Input: s = "aaaaa"
Output: 4

"aaaa" starts at index 0 and at index 1. The two occurrences overlap, which is allowed. The whole string of length 5 occurs only once.