Alphabetically Smallest Palindrome
Reported by candidates from Microsoft's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Microsoft reported this one in July 2026, and the detail that trips people is right in the example: "azzzbbb" becomes "abzbzba" after changing a single z to a. You're not fixing a string in place. You're changing letters so the multiset can be rearranged into a palindrome, then building the smallest one. It's a counting problem wearing a two-pointer costume. If you blank on the order of operations, StealthCoder runs invisibly during the live OA and can hand you the approach as a safety net. Read this first so you don't need it.
The problem
A palindrome reads the same from either direction; for example, ada is a palindrome. You are given a string s of lowercase English letters. Change the fewest letters possible so that the characters of s can be rearranged to form a palindrome. If multiple palindromes require the same minimum number of changes, return the alphabetically smallest one. Function makeAlphabeticallySmallestPalindrome(s: String) → String Examples Example 1 s = "azzzbbb" return = "abzbzba" Optimally, change one z to a to get aazzbbb. These letters can be rearranged to form abzbzba, which is the alphabetically smallest palindrome possible after one change. Constraints 1 ≤ s.length ≤ 3 × 10^5 s consists of lowercase English letters only.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Count the frequency of each letter. Letters with odd counts are the problem. A palindrome allows at most one odd count, and only if the length is odd. So pair up the odd-count letters: the number of changes is the number of odd letters divided by 2, rounded down. To keep the result smallest, take the largest odd letter and change it to the smallest odd letter, since the change moves a count from one to the other. Two pointers over the sorted odd letters does this cleanly. Then build the palindrome: put half of each letter's count in ascending order on the left, the odd middle letter if any, and mirror the left half on the right. The common pitfall is fixing the string positionally, which gives the wrong answer. Another is forgetting the middle letter choice when the length is odd. Do it in O(n) with a 26-slot array. A string of 3 x 10^5 means no sorting of characters is needed.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Alphabetically Smallest Palindrome cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Microsoft's OA.
Microsoft reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Alphabetically Smallest Palindrome FAQ
What's the trick in Alphabetically Smallest Palindrome?+
Think in counts, not positions. You only need the letter frequencies. Odd-count letters decide the number of changes, and you fix them by converting the largest odd letter into the smallest odd letter. Then build the palindrome from the sorted counts.
How many changes are needed at minimum?+
Count the letters with odd frequency. Each change can fix two of them by moving one unit from one letter to another. So you need floor(odd letters / 2) changes. One leftover odd letter is fine when the length is odd, and it becomes the middle.
How do I get the alphabetically smallest result?+
Two things matter. Change the largest odd letters into the smallest odd letters, so the small letters gain counts. Then lay out half of each letter's count in ascending order on the left side, mirror it on the right, and drop the middle letter in the center.
What complexity does Microsoft expect here?+
Length goes up to 3 x 10^5, so you need linear time. A 26-slot frequency array plus a single build pass is O(n) time and O(1) extra space beyond the output. Anything that sorts the whole string repeatedly or tries every change is too slow.
How do I prepare for this in 48 hours?+
Write it once from scratch. Count frequencies, pair up odd letters with two pointers, then build the left half and mirror it. Test with "azzzbbb" and an even-length case with no odd letters. Also try a single-character string and an all-same-letter string, since those edge cases are where most people slip.