Coding · should-tier · asked at Amazon, Apple, Google, Meta, Microsoft, Netflix
Reorganize String
Problem
Given a string s of lowercase letters, rearrange its characters so that no two adjacent characters are the same. Return any such arrangement, or the empty string if none exists.
- The string is up to 600 characters.
sconsists of lowercase English letters only.
Examples
s = "ccd"→"cdc". The twocs must be separated by thed.s = "cccd"→"". Threecs need at least two separators; only one other character exists.s = "wwwko"→"wkwow"(or another valid arrangement). Any arrangement with the threews spread out is accepted.
Hints
- Which character is the hardest to place? What is the maximum count it can have for a solution to exist at all?
- At each step, place the character with the most remaining copies, unless it is the one you just placed. Why is "most remaining" the safe choice?
- Max-heap of (count, char); pop the top, place it, and only push back the previous character after the current one is placed, so the same letter is never chosen twice in a row.
Last reviewed 8 September 2026
Locked
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Secure payment by Stripe · instant access · refund questions: [email protected]
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.