Coding questions · Meta
Check near-palindrome with one deletion
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.
Given a string s, determine whether it can become a palindrome after deleting at most one character. Return both the boolean result and one valid index to delete if possible. Provide an O(n) two-pointer solution and discuss how you would handle Unicode or case-insensitive comparisons.
What a Strong Answer Covers
- A clear algorithm with the right data structures and enough pseudocode or code-level detail to implement it.
- A correctness argument that explains why the algorithm covers all required cases.
- Time and space complexity, plus at least one alternative approach when relevant.
- Focused tests for normal cases, edge cases, and failure modes.
Follow-up Questions
- How would the approach change if the input were streaming or too large for memory?
- What invariants would you assert in production code?
- Which tests would catch off-by-one, duplicate, or tie-breaking bugs?
Check near-palindrome with one deletion
Given a string s, determine whether it can become a palindrome after deleting at most one character.
Return a pair [result, index]:
resultis a boolean:trueifsis already a palindrome or can be made one by deleting a single character, otherwisefalse.indexis one valid 0-based index you could delete to makesa palindrome. Ifsis already a palindrome (no deletion needed) or no single deletion works, return-1for the index.
Use the classic two-pointer scan: walk inward from both ends; on the first mismatch, the only two candidates are deleting the left or the right character, so check whether either resulting substring is a palindrome. This runs in O(n) time and O(1) extra space.
Follow-up to discuss (not graded here): for Unicode you should normalize (e.g. NFC) and iterate over code points / grapheme clusters rather than UTF-16 code units; for case-insensitive comparison, casefold the string first — both are preprocessing steps that keep the core two-pointer logic unchanged.
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
Example 1
Input: ("aba",)
Output: [True, -1]
Explanation: Already a palindrome, so no deletion is needed; index is -1.
Example 2
Input: ("abca",)
Output: [True, 1]
Explanation: First mismatch at b vs c; deleting index 1 ('b') leaves 'aca', a palindrome.
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.
Solution
Solution Alignment
The prompt asks for an implementation-level answer. The safest way to present it is to define the state, maintain clear invariants, then walk through complexity and tests.
Problem Restatement
Given a string s, determine whether it can become a palindrome after deleting at most one character. Return both the boolean result and one valid index to delete if possible. Provide an O(n) two-pointer solution and discuss how you would handle Unicode or case-insensitive comparisons.
Recommended Approach
Use the string constraints to choose between two pointers, a stack, frequency counts, prefix/suffix state, or dynamic programming. Maintain the invariant that processed characters have already been normalized, counted, or matched according to the operation.
Correctness
The implementation should maintain an invariant after each loop or operation that directly matches the problem statement. At termination, that invariant implies the returned value has considered every valid candidate exactly once, or has preserved the required data-structure state after every API call.
Complexity
Most direct string scans are O(n) time. Space ranges from O(1) for two pointers to O(n) for stacks, maps, or DP tables.
Edge Cases and Tests
Empty string, length 1, repeated characters, invalid characters, case sensitivity, Unicode vs ASCII, and very long input.