The Longest Palindromic Substring interview question gives you a string s and asks you to find the longest contiguous sequence of characters that reads the same forwards and backwards. For instance, in the string "babad", the longest palindromic substring is "bab" (or "aba", which is equally valid).
This is one of the most famous string problems in tech interviews. It tests a candidate's ability to recognize that brute-forcing all substrings takes time, which is unacceptable. Interviewers want to see if you can optimize the search to using the "Expand Around Center" technique or Dynamic Programming. It is a fantastic benchmark for identifying clean, off-by-one-free coding skills.
The most practical and intuitive approach is the Two Pointers (Expand Around Center) pattern. Because a palindrome mirrors around its center, there are possible centers in a string of length (a center can be a single character, or the space between two characters). You iterate through every possible center and use two pointers to expand outwards as long as the characters match.
Consider the string "cbbd".
We iterate through centers:
"c". Length 1."b". Length 1."bb". Length 2."bb".A very common mistake is forgetting that palindromes can have an even length (like "abba"). If candidates only write logic to expand from a single character, they will completely miss all even-length palindromes. You must explicitly handle both the single-character center expand(i, i) and the two-character center expand(i, i+1).
To ace the Longest Palindromic Substring coding problem, memorize the "Expand Around Center" helper function. It is much more space-efficient ( space) than the 2D Dynamic Programming solution ( space) and easier to write under pressure. Keep your bounds checking clean: while left >= 0 and right < len(s) and s[left] == s[right].
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Palindromic Substrings | Medium | Solve | |
| Push Dominoes | Medium | Solve | |
| Is Subsequence | Easy | Solve | |
| Longest Palindrome After Substring Concatenation I | Medium | Solve | |
| Find the Lexicographically Smallest Valid Sequence | Medium | Solve |