Break a palindrome to smallest non-palindrome
Company: Akuna Capital
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Take-home Project
Quick Answer: This question evaluates string manipulation, lexicographic ordering, and algorithmic reasoning, including careful edge-case handling and time and space complexity analysis. Commonly asked in the Coding & Algorithms domain, it assesses practical application of algorithm design and correctness reasoning with emphasis on conceptual understanding and complexity trade-offs.
Constraints
- 1 <= len(s) <= 100000
- s consists only of lowercase English letters
- s is guaranteed to be a palindrome
Examples
Input: ("abccba",)
Expected Output: "aaccba"
Explanation: Changing the first non-'a' character in the first half ('b' at index 1) to 'a' gives 'aaccba', which is not a palindrome and is the smallest possible.
Input: ("a",)
Expected Output: ""
Explanation: A single-character string remains a palindrome no matter which character it is changed to, so it is impossible.
Input: ("aa",)
Expected Output: "ab"
Explanation: The first half contains only 'a', so changing the last character to 'b' produces the smallest non-palindrome.
Input: ("aba",)
Expected Output: "abb"
Explanation: The only character in the first half is already 'a', so changing the last character to 'b' gives 'abb'.
Input: ("abba",)
Expected Output: "aaba"