Rearrange Binary String
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon's October OA hit candidates with a string manipulation problem that looks deceptively simple. You get a binary string and need to rearrange it according to a specific rule. The trap is assuming brute force will work or that you can solve it in one pass. You can't. This is a classic two-pass or greedy counting problem, and StealthCoder has your back if you blank on the exact pattern during the live assessment.
Pattern and pitfall
The pattern here is greedy counting with a secondary constraint. Without seeing the exact rearrangement rule, the core mechanic is almost certainly: count your 0s and 1s, then reconstruct the string in a way that satisfies a local or global ordering requirement. Common variants include alternating bits, isolating groups, or satisfying adjacency rules. The mistake most candidates make is trying to rearrange in place or thinking a single scan works. You need to either count first then build, or simulate the rearrangement step by step. StealthCoder will read the full problem on your screen and surface the reconstruction logic immediately so you're not guessing at the rule.
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 Rearrange Binary 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. 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 Amazon's OA.
Amazon 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.
Rearrange Binary String FAQ
Is this really just counting 0s and 1s?+
Counting is almost always step one, but the rearrangement rule is the real test. The question is what constraint governs where each character goes. Read the problem carefully for phrases about 'adjacent', 'alternating', 'groups', or explicit ordering. That constraint determines your algorithm.
Do I need dynamic programming here?+
Unlikely. This is a string manipulation problem, not a state-space search. DP would be overkill. Think greedy counting, two-pointer reconstruction, or simulation. If you're reaching for DP on a string rearrangement problem, step back and re-read the constraint.
How much can I optimize on the first attempt?+
Amazon usually accepts a clean two-pass solution: count in pass one, build in pass two. That's O(n) time and O(n) space for the output. Don't overthink it. Get it right first, then optimize if they push.
What's the most common edge case Amazon tests?+
Empty string, all 0s, all 1s, and alternating strings. Also watch for off-by-one errors in your reconstruction loop. Test your loop bounds carefully before submitting.
Can I solve this in 10 minutes?+
Yes, if you identify the rearrangement rule quickly. The actual coding is straightforward: count, validate the rule against your count, then build the result string. Time crunch is usually reading comprehension, not algorithm complexity.