Find the longest palindromic substring by expanding around every centre in O(n²) time and O(1) space, handling odd and even lengths. Common FAANG medium.
The problem
Return the longest substring of s that is a palindrome. If several share the longest length, return the one that starts first.
Examples
Example 1
Input
longest_palindrome('babad')Expected output
'bab'
Example 2
Input
longest_palindrome('cbbd')Expected output
'bb'
+ 4 hidden tests on Submit.
Edge cases to ask about
- Even-length palindromes
- Single character
- Empty string
Hints
0/3How an interviewer scores this
0/9Your code runs in real CPython inside your browser — nothing is sent anywhere. The first run downloads the interpreter (about 6 MB, once). Your code is saved on this device as you type.
Complexity Lab
What does this cost as n grows?
Interviewers score the analysis as much as the code. Commit to an answer first — then check it, and measure your code against the optimal one at growing input sizes.
Pick both to reveal the answer.
Measure it
Runs the function on inputs of size 250 up to 16,000 and records the time and peak memory. Slow solutions stop early — a short curve is itself the answer.
From brute force to optimal
The progression an interviewer wants to hear, one step at a time.
| Approach | Time | Space | Idea |
|---|---|---|---|
| Check every substring | O(n³) | O(1) | |
| DP table is_pal[i][j] | O(n²) | O(n²) | |
| Expand around 2n − 1 centres | O(n²) | O(1) | Odd centres are characters, even centres are gaps. |
| bestManacher's algorithm | O(n) | O(n) | Rarely expected — mention it. |
Walkthrough of the optimal approach (try it yourself first)
Every palindrome mirrors around a centre — a single character (odd length) or the gap between two characters (even length). From each of the 2n − 1 centres, expand while the ends match and record the longest. O(n²) time, O(1) space.
Forgetting the even-length centres is the usual bug ("cbbd" → "bb").
Complexity: O(n²) time, O(1) space. There are 2n − 1 centres and each expansion can take O(n).
Reveal the reference solution
def longest_palindrome(s): best_start, best_len = 0, 0 for centre in range(len(s)): for lo, hi in ((centre, centre), (centre, centre + 1)): while lo >= 0 and hi < len(s) and s[lo] == s[hi]: lo -= 1 hi += 1 length = hi - lo - 1 if length > best_len: best_start, best_len = lo + 1, length return s[best_start:best_start + best_len]
Follow-ups interviewers ask
- Count all palindromic substrings.
- Explain Manacher's O(n) idea.
Frequently asked interview questions
Core interview concepts, complexities, and follow-ups scored by hiring teams.
What is the time complexity of Longest Palindromic Substring in Python?
The optimal solution runs in O(n²) time and O(1) auxiliary space. There are 2n − 1 centres and each expansion can take O(n).
What is the brute-force approach, and how do you optimise it?
Check every substring: O(n³) time, O(1) space. DP table is_pal[i][j]: O(n²) time, O(n²) space. Expand around 2n − 1 centres: O(n²) time, O(1) space. Odd centres are characters, even centres are gaps. Manacher's algorithm: O(n) time, O(n) space. Rarely expected — mention it.
What follow-up questions do interviewers ask about Longest Palindromic Substring?
Count all palindromic substrings. Explain Manacher's O(n) idea.
