Coding · must-tier · asked at Amazon, Apple, Google, Meta, Microsoft, Netflix
Longest Palindromic Substring
Problem
Given a string s, return the longest contiguous substring of s that reads the same forwards and backwards. If several substrings tie for longest, any one of them is acceptable.
- The string is up to 1,200 characters.
sconsists of digits and English letters.
Examples
s = "xyzabaqr"→"aba". An odd-length palindrome centred on the middleb.s = "xyabbaq"→"abba". An even-length palindrome; its centre sits between the two b's.s = "q"→"q". A single character is trivially a palindrome.
Hints
- Every palindrome has a centre. How many possible centres does a string of length n have, counting the gaps between characters?
- From a given centre, how far can you grow outward while the ends still match? Does growing ever undo a smaller palindrome?
- Expand around each of the 2n-1 centres; keep the widest. Alternatively,
dp[i][j] = s[i]==s[j] and dp[i+1][j-1].
Last reviewed 8 September 2026
Locked
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Secure payment by Stripe · instant access · refund questions: [email protected]
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.