Longest Palindromic Substring
Updated
Problem statement
Given a string s, return its longest contiguous substring that reads the same from left to right and from right to left.
If more than one longest palindromic substring exists, return the one whose first character appears earliest in s.
Function
longestPalindrome(s: String) → StringExamples
Example 1
s = "babad"return = "bab"Both bab and aba have maximum length three. The earlier one is bab.
Example 2
s = "cbbd"return = "bb"The two middle characters form the unique longest palindrome.
Example 3
s = "forgeeksskeegfor"return = "geeksskeeg"The ten-character middle substring is palindromic and no longer substring is.
Constraints
1 <= s.length <= 2000.scontains only uppercase English letters, lowercase English letters, and digits.- Character comparisons are case-sensitive.
More eBay interview questions
- Meeting Rooms IIPHONE SCREEN · Seen Oct 2026
- Construct a Binary Tree from Preorder and InorderPHONE SCREEN · Seen Oct 2026
- Maximum Size Subarray Sum Equals KPHONE SCREEN · Seen Sep 2026
- K Smallest Elements from Sorted ArraysPHONE SCREEN · Seen Jul 2026
- Single-Pass Search in a Rotated Sorted ArrayPHONE SCREEN · Seen Jul 2026
- Xiangqi Horse ReachabilityPHONE SCREEN · Seen Jul 2026
- Extract Hashtags from a TweetONSITE INTERVIEW · Seen Nov 2026
- Validate Ada Integer LiteralONSITE INTERVIEW · Seen Nov 2026
- Count Distinct Pairs With Sum KONSITE INTERVIEW · Seen Nov 2026
- Diameter of Binary TreeONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Longest Palindromic SubstringSiemens · PHONE SCREEN
- Query Words Within K PositionsPalantir · PHONE SCREENPalantir interview questions
- Edit DistanceDP World · PHONE SCREEN
- Longest Palindromic SubstringIBM · PHONE SCREENIBM interview questions
- Word BreakAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Decode WaysThe D.E. Shaw Group · PHONE SCREENThe D.E. Shaw Group interview questions
- Edit DistanceAstrotalk · PHONE SCREEN
- Validate a Palindrome After Limited DeletionsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Count Palindromic SubstringsThe Voleon Group · PHONE SCREEN
- Generate One-Two Strings with Sum NGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions