Data Structures & AlgorithmsVery hardProduct Company SDE · Amazon/Microsoft-style OA

Problem: Find the longest substring that appears at least twice in a string of length 10⁵. Which approach is efficient?

  1. A. Compare all pairs of substrings – O(n³)
  2. B. Sliding window with a frequency map of characters
  3. C. Binary search on the length L plus rolling hashes of all length-L substrings – O(n log n) expected (or a suffix array with LCP)
  4. D. KMP of the string against itself – O(n)

Correct answer: C. Binary search on the length L plus rolling hashes of all length-L substrings – O(n log n) expected (or a suffix array with LCP)

If a repeated substring of length L exists, one of every shorter length exists too, so L is monotonic and binary-searchable; checking a length is O(n) with rolling hashes. A suffix array plus LCP array gives the answer as the maximum LCP.

More Coding Interview Problems questions

Exams that ask this topic