Longest palindrome in a name

strings

Longest palindrome in a name

Airbnb Python Interview Question

Airbnb's naming team looks for the longest part of a listing name that reads the same forwards and backwards, for a playful search feature.

Write a function longest_palindrome(text) that returns the longest stretch of consecutive characters that reads the same in both directions. If several stretches tie, return the one that starts first. The text has at least one character.

Asked of

  • Data Analyst
  • Data Engineer
  • Data Scientist
  • ML Engineer
  • AI Engineer

Example 1

Input

text = "babad"

Output

"bab"

Example 2

Input

text = "cbbd"

Output

"bb"

Explanation

In the first example, "bab" and "aba" both read the same in both directions and have 3 letters, and "bab" starts first. In the second example, "bb" is the longest such stretch.

Submit also runs 4 hidden test cases that check edge cases.