Reorganize a String
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The detail that trips people up in this Amazon OA, reported September 2026, is the tie-break rule. It isn't just "rearrange so no two neighbors match." You must pick the highest-frequency eligible character at each step, and break ties by smallest letter. That makes the output deterministic, so any valid arrangement fails the judge. It's a string problem with a greedy heap underneath, and with up to 10^5 characters you need something near O(n log 26). If you blank on the mechanics, StealthCoder runs invisibly during the live assessment and gives you the working solution as a safety net.
The problem
Rearrange a lowercase string so that no two adjacent characters are equal. If no such arrangement exists, return the empty string. To make the judged result deterministic, construct the answer with this rule: at each position, select the eligible character with the greatest remaining frequency. The previously placed character is not eligible. If several eligible characters have the same frequency, select the lexicographically smallest one. Function reorganizeString(s: String) → String Examples Example 1 s = "aab" return = "aba" The highest-frequency eligible character is chosen first, producing a valid arrangement. Example 2 s = "aaab" return = "" Three copies of one character cannot be separated by the single remaining character. Example 3 s = "aabbcc" return = "abcabc" Frequency ties are resolved in lexicographic order while the previous character remains temporarily ineligible. Constraints 1 <= s.length <= 10^5 s contains only lowercase English letters.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Count frequencies of the 26 letters. At each position, pick the eligible letter with the highest remaining count, skipping the one just placed, and break ties alphabetically. A max-heap keyed on (-count, letter) does this cleanly. Pop the top, append it, then hold it out for one step. Push the previously held letter back only if its count is still above zero. If the heap is empty and the result isn't full length, return the empty string. The quick feasibility check is max frequency greater than (n+1)/2, which means impossible. The common pitfall is pushing the used letter straight back, which allows adjacent duplicates. Another is using a plain greedy without the lexicographic tie-break, which gives valid but wrong output for aabbcc. Check your result against abcabc. If the heap logic falls apart under pressure, StealthCoder is the hedge during the live OA.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Reorganize a String 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. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as reorganize string. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Reorganize a String FAQ
What's the trick to Reorganize a String?+
Greedy with a max-heap. Always place the most frequent letter that isn't the one you just placed. Hold the last-used letter out for one step, then push it back if it still has count left. That prevents adjacent duplicates and keeps the order deterministic.
How do I know when to return the empty string?+
If any letter appears more than (n+1)/2 times, it can't be separated, so return empty. You can also detect it during the build: if the heap is empty but the result isn't full length, bail out. Both approaches give the same answer.
Why does the tie-break matter so much here?+
This version judges exact output, not just validity. For aabbcc the answer must be abcabc. Store heap entries as (-count, letter) so equal counts resolve to the smallest letter. Without that, you'll pass the basic cases and fail the hidden ones.
What's the time complexity I should aim for?+
With only 26 lowercase letters, each heap operation is effectively constant, so you get O(n log 26), which is basically linear. Counting is O(n). That's comfortably fine for 10^5 characters. Avoid anything that rescans the whole string at each position.
How do I prepare for this in 48 hours?+
Write the heap solution from scratch twice. Test aab, aaab, and aabbcc by hand, especially the tie-break on the last one. Then try a single-character string and an all-same-letter string. Those edge cases are where most wrong answers come from.