Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Longest Palindrome
00:00
5 left

Longest Palindrome

MediumPython

Problem

A Sumo Logic backend service receives a compact log fragment as a string. Return its longest contiguous substring that is a palindrome, meaning it reads identically from left to right and right to left.

If multiple palindromic substrings have the maximum length, return the one with the smallest starting index.

Formal Specification

Implement longest_palindrome(s).

  • Input: s, a string containing lowercase English letters.
  • Output: A string containing the longest palindromic substring of s.

A palindrome must be contiguous. For example, "aba" is valid within "caba", but "aa" formed from non-adjacent characters is not a substring.

Constraints

  • 1 <= len(s) <= 2000
  • s contains only lowercase English letters
  • The palindrome must be a contiguous substring
  • If multiple answers have the same maximum length, return the leftmost one

Function Signature

def longest_palindrome(s):
Interviewer

Your question is Longest Palindrome. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.