MediumStringDynamic ProgrammingUpdated Sep 2026

5. Longest Palindromic Substring

Asked at Google, Meta, Amazon, Apple and 8 more

Difficulty

Medium

Time

O(n^2)

Space

O(1)

Pattern

String

Companies

12

Problem

You are given a string s. Return the longest substring of s that reads the same forwards and backwards (a palindrome).

A substring must be a contiguous block of characters — you cannot skip characters. If several substrings tie for the longest length, any one of them is accepted as the answer.

A single character is always a palindrome, so the answer always has length at least 1.

Find the longest mirrored block

palindromebabad01234"bab" mirrors around the middle 'a'also length 3babad01234"aba" is just as long, so it is an equally good answer
In "babad" the longest block that reads the same both ways is "bab" (length 3).

Constraints

  • ●1 <= s.length <= 1000
  • ●s consists of only digits and English letters

Examples

Example 1
Inputs = "babad"
Outputbab
Why"bab" reads the same in both directions and has length 3. "aba" is also length 3 and also valid, so either answer is accepted.
Example 2
Inputs = "cbbd"
Outputbb
WhyThe only palindrome longer than one character is "bb", which has length 2.

Asked At

12 companies

How to Think About It

1

Brute force: check every substring. There are n*(n+1)/2 substrings. For each, check if it's a palindrome in O(n). Total: O(n³). Too slow.

2

Better: dynamic programming. dp[i][j] = true if s[i:j+1] is a palindrome. Build from shorter to longer. O(n²) time and space. Works but space-heavy.

3

Best: expand around center. A palindrome mirrors around its center. For each possible center, expand outward while characters match. Track the longest one found.

4

Why expand-around-center works: every palindrome has a center. Odd-length palindromes have a single character center (e.g., "aba" centered at 'b'). Even-length have a gap center (e.g., "abba" centered between the two 'b's). So there are 2n-1 possible centers.

5

Edge cases: single character (return it), all same characters (return entire string), no palindrome longer than 1 (return first char).

Optimal Approach

For each index i in the string:

  1. Odd-length palindrome: expand from center i. Set left = i, right = i. While left >= 0 and right < n and s[left] == s[right], expand outward (left--, right++). Record the palindrome.

  2. Even-length palindrome: expand from center between i and i+1. Set left = i, right = i+1. Same expansion logic.

  3. After checking both centers at each position, update the longest palindrome found.

The expand function returns the palindrome string. We compare lengths and keep the longest.

Time: O(n²) — n centers, each expansion takes O(n) worst case. Space: O(1) — just pointers, no extra data structures.

Expand around every center

babad01234l=rStep 1 — center on index 1 ('a'), a length-1palindromebabbabad01234lrStep 2 — s[0]='b' equals s[2]='b', so expand to "bab"best = 3babad01234centerStep 3 — next step compares 'b' and 'a', they differ,so stop and keep "bab"odd center: l = r = ieven center: l = i, r = i + 1while s[l] == s[r]: l--, r++length = r - l - 1
Try each character as an odd center and each gap as an even center, then grow outward while the two ends match.
ComplexityTime O(n^2)Space O(1)There are 2n-1 centers, and each expansion can walk up to n characters, while only a few pointers are stored.

What Trips People Up in Real Interviews

1

Confusing "substring" with "subsequence." A substring is contiguous. "babad" has "bab" as a substring palindrome, but "aceca" is not a substring of "abcba" — it's a subsequence.

2

Trying to use dynamic programming on the full string without realizing that expand-from-center is simpler and O(1) space. For each position, try expanding outward from (i, i) and (i, i+1).

3

Forgetting to handle both odd-length and even-length palindromes. "aba" is odd-length (center is b), "abba" is even-length (center is between the two b's). You need both.

4

Not updating the result correctly. Keep track of the start index and max length, not just the length. When you find a longer palindrome, update both.

5

Returning s[l:r] from the expand function instead of s[l+1:r]. After the while loop exits, l and r have each moved one step past the valid palindrome boundary.

Solution Code

O(n^2) time
def longestPalindrome(s):
    def expand(l, r):
        while l >= 0 and r < len(s) and s[l] == s[r]:
            l -= 1
            r += 1
        return s[l+1:r]

    best = ""
    for i in range(len(s)):
        odd = expand(i, i)
        even = expand(i, i + 1)
        best = max(best, odd, even, key=len)
    return best

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Longest Palindromic Substring problem?

Given a string, find the longest substring that is a palindrome. This problem tests your understanding of expand-around-center or dynamic programming approaches and is a staple of FAANG interviews.

How do you solve Longest Palindromic Substring?

The optimal approach runs in O(n^2) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Longest Palindromic Substring?

Longest Palindromic Substring is asked at Google, Meta, Amazon, Apple, Microsoft, Oracle, Adobe, Salesforce, Walmart, Bloomberg, Goldman Sachs, Visa. It is a medium difficulty problem.

What are common mistakes on Longest Palindromic Substring?
  • Confusing "substring" with "subsequence." A substring is contiguous. "babad" has "bab" as a substring palindrome, but "aceca" is not a substring of "abcba" — it's a subsequence.
  • Trying to use dynamic programming on the full string without realizing that expand-from-center is simpler and `O(1)` space. For each position, try expanding outward from (i, i) and (i, i+1).
  • Forgetting to handle both odd-length and even-length palindromes. "aba" is odd-length (center is b), "abba" is even-length (center is between the two b's). You need both.
  • Not updating the result correctly. Keep track of the start index and max length, not just the length. When you find a longer palindrome, update both.
  • Returning `s[l:r]` from the expand function instead of `s[l+1:r]`. After the while loop exits, l and r have each moved one step past the valid palindrome boundary.