- Interview Questions
- /
- Problems
- /
- Valid Palindrome II
Valid Palindrome II
Decide whether a string can become a palindrome by deleting at most one character. Two pointers from both ends, with a single branch at the first mismatch. Solve it below in Python or JavaScript, then read the walkthrough.
Problem
You're given a string s of lowercase letters. You may delete at most one character from it, anywhere.
Return true if the string can read the same forwards and backwards after that, and false otherwise. A string that is already a palindrome counts as true with no deletion.
Example 1
- Input
- s = "madxam"
- Output
- true
Delete the "x" to get "madam".
Example 2
- Input
- s = "abbab"
- Output
- true
The ends "a" and "b" differ. Deleting the last "b" gives "abba". Deleting the first "a" gives "bbab", which does not work, so you have to try both sides.
Example 3
- Input
- s = "tebbem"
- Output
- false
The first and last characters, "t" and "m", differ. Deleting "t" leaves "ebbem" and deleting "m" leaves "tebbe", and neither is a palindrome.
Constraints
- 1 ≤ s.length ≤ 10⁵
- s contains only lowercase English letters.
- At most one character may be deleted.
Practice
Runs 15 tests in your browser. Python loads on the first run (about 6 MB, then cached). Press ⌘/Ctrl + Enter to run.
Get asked Valid Palindrome II in a real interview?
Interview Solver gives you a working solution live, during your actual coding interview. Invisible to screensharing.
How to solve Valid Palindrome II
Pattern: Two pointers with one branch
Checking a palindrome is two pointers walking inward and comparing. As long as the characters match, there is nothing to decide: deleting a matched character would never help.
The first mismatch, at positions left and right, is the only place a deletion can go. One of those two characters must be the extra one, so there are exactly two candidates: skip s[left] and check whether s[left+1..right] is a palindrome, or skip s[right] and check s[left..right-1]. If either works, the answer is true. No further deletions are allowed, so those inner checks are plain palindrome checks.
Approach 1: Try deleting every character
For each index, build the string without it and test whether it is a palindrome. Also test the original string. Simple, but it does n palindrome checks of length n.
Time O(n²) · Space O(n)
Approach 2: Two pointers, branch once at the first mismatch
- Move
leftup from 0 andrightdown fromn - 1while the characters match. - If the pointers meet, the string is already a palindrome.
- Otherwise return whether
s[left+1..right]ors[left..right-1]is a palindrome, checked with the same inward scan on index ranges (no copying needed).
The outer scan plus at most two inner scans touch each character a constant number of times.
Time O(n) · Space O(1)
| Approach | Time | Space |
|---|---|---|
| Try deleting every character | O(n²) | O(n) |
| Two pointers, branch once at the first mismatch | O(n) | O(1) |
Solution code
Try the practice editor first. Both solutions pass every test above.
Valid Palindrome II solution in Python
def valid_palindrome(s: str) -> bool:
def is_pal(lo: int, hi: int) -> bool:
while lo < hi:
if s[lo] != s[hi]:
return False
lo += 1
hi -= 1
return True
left, right = 0, len(s) - 1
while left < right:
if s[left] != s[right]:
# The extra character is one of these two.
return is_pal(left + 1, right) or is_pal(left, right - 1)
left += 1
right -= 1
return TrueValid Palindrome II solution in JavaScript
function validPalindrome(s) {
const isPal = (lo, hi) => {
while (lo < hi) {
if (s[lo] !== s[hi]) return false;
lo++;
hi--;
}
return true;
};
let left = 0;
let right = s.length - 1;
while (left < right) {
if (s[left] !== s[right]) {
// The extra character is one of these two.
return isPal(left + 1, right) || isPal(left, right - 1);
}
left++;
right--;
}
return true;
}Common mistakes
- Only trying one side at the mismatch. In "abbab" you must delete on the right; in "babba" you must delete on the left.
- Continuing the outer loop after the first deletion and allowing a second one later.
- Building substrings for the inner checks. It works, but compare by index to keep O(1) extra space.
- Forgetting that a string that is already a palindrome should return true.
What interviewers look for
Explain why the mismatch is the only decision point: matched outer characters are safe to keep, so the deletion has to be one of the two mismatched characters. That one sentence justifies the whole algorithm.
Then show a case where each side is the right one ("abbab" and "babba"). Interviewers often probe whether a greedy "always skip left" works, and having those two examples ready answers it immediately.
Follow-up questions
What if you could delete up to k characters?
Recursion with a budget branches at every mismatch, which is exponential without caching. Memoize on (left, right) with the deletions used, or compute the longest palindromic subsequence with DP; the string works if n minus that length is at most k. That DP is O(n²).
What if the string had mixed case and punctuation to ignore?
Skip non-alphanumeric characters with the pointers and compare lowercase forms, as in the basic Valid Palindrome. The single-branch logic stays the same.
How would you return which character to delete?
At the mismatch, return left if s[left+1..right] is a palindrome, otherwise right if s[left..right-1] is. If there is no mismatch, no deletion is needed; return -1 or any middle index.
Companies that ask Valid Palindrome II
Valid Palindrome II is tagged by 94 companies in our interview dataset. The score shows how often it comes up at each company.
- Billcom100%
- Opendoor100%
- Whatnot100%
- Basis Technologies97%
- Boston Scientific96%
- Adevinta96%
- Meta95.1%
- Coalition95%
- Amrock94%
- Esri93%
- Cargurus92%
- Springworks Therapeutics90%
- Fortinet89.1%
- Scout2486%
- Coffee Meets Bagel85%
- Eightfold AI84%
- Arcadia84%
- Bairesdev82%
- Yandex80.3%
- Etsy80%
- Murex80%
- Roku79.3%
- Fico79%
- Binance75%
and 70 more.
Practice next
Get live help in the real interview
Working solutions in real time during your actual interview. Invisible to screensharing.
Start Free TrialNo credit card required