PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

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.

  • Medium
  • Akuna Capital
  • Coding & Algorithms
  • Software Engineer

Break a palindrome to smallest non-palindrome

Company: Akuna Capital

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Take-home Project

Given a palindromic string s of lowercase English letters, change exactly one character to obtain a new string that is not a palindrome and is lexicographically smallest among all such possibilities. If no such string exists, return an empty string. Explain your algorithm, prove correctness, and analyze time and space complexity. Provide code in your preferred language.

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.

You are given a palindromic string s consisting of lowercase English letters. Change exactly one character so that the resulting string is not a palindrome and is lexicographically smallest among all such possible strings. If it is impossible to make the string non-palindromic by changing exactly one character, return an empty string. A string a is lexicographically smaller than string b if at the first position where they differ, a has a smaller character than b.

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"

Explanation: Changing the 'b' in the first half to 'a' creates 'aaba', which is smaller than any valid alternative.

Input: ("zzzz",)

Expected Output: "azzz"

Explanation: Replacing the earliest character with 'a' gives the smallest lexicographic result and breaks the palindrome immediately.

Solution

def solution(s):
    n = len(s)
    if n == 1:
        return ""

    chars = list(s)

    # Make the earliest possible character as small as possible.
    # We only need to inspect the first half; changing a non-middle
    # character there to 'a' will break the palindrome.
    for i in range(n // 2):
        if chars[i] != 'a':
            chars[i] = 'a'
            return ''.join(chars)

    # If the first half is all 'a', the smallest valid change is to
    # increase the last character to 'b'.
    chars[-1] = 'b'
    return ''.join(chars)

Time complexity: O(n). Space complexity: O(n).

Hints

  1. To make the result lexicographically smallest, try to decrease the earliest possible character.
  2. Only characters in the first half need to be checked for replacing with 'a'. If they are all already 'a', think about changing the last character instead.
Last updated: Apr 23, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Compute Graph Spread and Portfolio Trades - Akuna Capital (medium)
  • Find minimum swaps to sort array with duplicates - Akuna Capital (hard)
  • Heapify an array into a max-heap - Akuna Capital (Medium)
  • Implement a ring buffer - Akuna Capital (Medium)
  • Compute max profit across dated stock quotes - Akuna Capital (Medium)