Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Longest Palindrome

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Longest Palindrome. Start with the requirements on the right.

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.

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):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output