Skip to content
LakeBench
ProblemsCommunityPricing
Sign inStart practicing

Longest palindromic substring

Python data engineering interview problem. Difficulty: advanced. Pattern: Strings. About 20 minutes. Part of the Pro drill bank.

Find the longest substring that reads the same forwards and backwards; the leftmost one on a tie. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.

Implement longest_palindrome(s: str) -> str. Return the longest contiguous substring of s that is a palindrome. If several have the same length, return the one that starts earliest. An empty string gives "". The string can have 1,000 characters, so checking every substring one by one is too slow.

Requirements

  • Leftmost on equal length.

Constraints

  • Up to 1,000 characters.
  • Case matters.

Examples

Input: longest_palindrome("cbbd") Output: 'bb' The even-length palindrome bb is the longest.

Topics: lakebench, python, palindrome, expand around center.

More Python interview questions · All interview problems · Learn data engineering

advanced

Longest palindromic substring

Interview-style drill: Find the longest substring that reads the same forwards and backwards; the leftmost one on a tie.

Implement `longest_palindrome(s: str) -> str`. Return the longest contiguous substring of `s` that is a palindrome. If several have the same length, return the one that starts earliest. An empty string gives `""`. The string can have 1,000 characters, so checking every substring one by one is too slow.