Palindrome After Deleting at Most One Character
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates a candidate's ability to reason about two-pointer string traversal and palindrome verification under a single edit constraint. It tests practical application of algorithmic thinking on strings, a staple of coding interviews for assessing efficient linear-time problem solving with constant extra space.
Constraints
- 1 <= s.length <= 10^5
- s consists only of lowercase English letters.
Examples
Input: "aba"
Expected Output: true
Explanation: Already a palindrome; no deletion needed.
Input: "abca"
Expected Output: true
Explanation: Delete 'c' to get "aba" (or 'b' to get "aca").
Input: "abcdef"
Expected Output: false
Explanation: No single deletion can make it a palindrome.
Input: ""
Expected Output: true
Explanation: The empty string is trivially a palindrome.
Input: "a"
Expected Output: true
Explanation: A single character is trivially a palindrome.
Input: "deeee"
Expected Output: true
Explanation: Delete the leading 'd' to get "eeee", a palindrome.
Input: "ebcbbececabbacecbbcbe"
Expected Output: true
Explanation: Mismatch at the 'e'/'c' pair; skipping that one character leaves a palindrome.
Input: "abc"
Expected Output: false
Explanation: Deleting any one character still leaves a non-palindrome.
Input: "cbbcc"
Expected Output: true
Explanation: Delete the trailing 'c' to get "cbbc", a palindrome.
Input: "racecar"
Expected Output: true
Explanation: Already a palindrome.
Hints
- Use two pointers, one at each end, moving inward while the characters match.
- When you hit the first mismatch, you have exactly one deletion to spend: it must remove either the left or the right character.
- Check whether the substring after skipping the left character is a palindrome, OR the substring after skipping the right character is a palindrome. If either holds, the answer is true.
- A string of length 0 or 1 is trivially a palindrome.