Find the Longest Palindromic Substring in Linear Time
Company: ByteDance
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: Implement `longest_palindromic_substring(s)` and achieve `O(n)` time. Work through the function contract, boundary cases, correctness argument, and time and space complexity expected in a production-quality solution.
Constraints
- 0 <= len(s) <= 200000.
- The string contains ASCII letters and digits.
- Return a contiguous palindrome; equal maximum lengths use the smallest starting index, and empty input returns the empty string.
Examples
Input: ('',)
Expected Output: ''
Explanation: Empty input returns empty output.
Input: ('a',)
Expected Output: 'a'
Explanation: A singleton is its own palindrome.
Hints
- Test empty and singleton inputs, an all-equal string, and a string with no palindrome longer than one character.
- Include both odd-length and even-length longest palindromes.
- Use two different maximum-length palindromes beginning at different positions and verify the earlier one wins.