Check near-palindrome with one deletion
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: This interview question evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer for Check near-palindrome with one deletion states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.
Constraints
- 0 <= len(s) <= 10^5
- s consists of lowercase (or arbitrary) characters
- An empty string and a single-character string are palindromes (return [True, -1])
- If s is already a palindrome, the deletion index is -1 (no deletion needed)
Examples
Input: ("aba",)
Expected Output: [True, -1]
Explanation: Already a palindrome, so no deletion is needed; index is -1.
Input: ("abca",)
Expected Output: [True, 1]
Explanation: First mismatch at b vs c; deleting index 1 ('b') leaves 'aca', a palindrome.
Input: ("abc",)
Expected Output: [False, -1]
Explanation: a vs c mismatch; neither 'bc' nor 'ab' is a palindrome, so one deletion is not enough.
Input: ("",)
Expected Output: [True, -1]
Explanation: Empty string is trivially a palindrome.
Input: ("a",)
Expected Output: [True, -1]
Explanation: Single character is a palindrome.
Input: ("deeee",)
Expected Output: [True, 0]
Explanation: d vs e mismatch at the ends; deleting index 0 ('d') leaves 'eeee', a palindrome.
Input: ("racecar",)
Expected Output: [True, -1]
Explanation: Already a palindrome; index is -1.
Input: ("abcdba",)
Expected Output: [True, 2]
Explanation: Outer pairs match down to c vs d; deleting index 2 ('c') leaves 'abdba', a palindrome.
Input: ("abcde",)
Expected Output: [False, -1]
Explanation: a vs e mismatch; neither 'bcde' nor 'abcd' is a palindrome, so it cannot be fixed with one deletion.
Hints
- Use two pointers from both ends. As long as characters match, move inward.
- At the first mismatch, only two repairs are possible: delete the left character or delete the right one. Check if either remaining substring is a palindrome.
- If you reach the middle with no mismatch, the string was already a palindrome — return -1 for the index. If both repair attempts fail, it's impossible.