Longest Palindromic Substring
Updated
Problem statement
Given a string s, return the longest consecutive part that reads the same forward and backward.
When several longest palindromes exist, return the one that starts earliest in s. Return the empty string for empty input.
Function
longestPalindrome(s: String) → StringExamples
Example 1
s = "babad"return = "bab"bab and aba both have length three; bab starts earlier.
Example 2
s = "cbbd"return = "bb"The longest palindrome is the middle pair.
Example 3
s = ""return = ""Empty input returns empty output.
Constraints
0 <= s.length <= 2000.scontains ASCII characters.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Validate a Palindrome After Limited DeletionsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Longest Palindromic Substring LengthMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Longest Palindromic SubstringSiemens · PHONE SCREEN
- Longest Palindromic SubstringIBM · PHONE SCREENIBM interview questions
- Longest Subsequence That Is a SubstringExpedia · OAExpedia interview questions
- Longest Palindromic SubstringInMobi · PHONE SCREENInMobi interview questions
- Longest Subsequence which is a SubstringSalesforce · OASalesforce interview questions
- Longest Properly Closed Bracket RunAdobe · PHONE SCREENAdobe interview questions
- Indexed Proximity SearchStripe · PHONE SCREENStripe interview questions
- Shortest Deletion for Distinct CharactersOracle · ONSITE INTERVIEWOracle interview questions