Reported July 2026
Microsoftstring

Binary String Swap Time

Reported by candidates from Microsoft's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Microsoft OA. Under 2s to a working solution.
Founder's read

The edge case that kills the naive answer on this Microsoft OA, reported in July 2026, is a string like "0011" where counting swaps or zeros gives the wrong time. The problem looks like a plain string simulation. Swap every "01" to "10" at once and count the rounds until none are left. Simulating works on the sample, then crawls on long inputs. Consecutive blue uniforms queue up behind each other, so the answer isn't just the number of zeros. If you blank on the faster approach during the live assessment, StealthCoder runs invisibly on your desktop and can hand you the linear solution. Here's the pattern so you don't need it.

The problem

In HackerLand, people are preparing for a parade. A red uniform must not be immediately to the left of a blue uniform.
You are given a binary string color, where:
'0' represents a person in a red uniform.
'1' represents a person in a blue uniform.
At each second, every occurrence of "01" is simultaneously changed to "10". Repeat this process until the string contains no occurrence of "01".
Return the number of seconds until the process stops.

Function
getSwapTime(color: String) → int

Examples
Example 1
color = "001011"
return = 4
The string changes as follows:
t = 0: 001011
t = 1: 010101
t = 2: 101010
t = 3: 110100
t = 4: 111000
After 4 seconds, no occurrence of "01" remains.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Every swap moves a 1 one step left past a 0. The final string is all 1s followed by all 0s. So the question is how long the last 1 takes to settle. Scan left to right and keep a count of zeros seen so far. For each '1' with at least one zero before it, set time = max(time + 1, zeros). The zeros count is the minimum that 1 needs to cross every zero. The time + 1 term covers the case where it's stuck behind the previous 1 and must wait one extra second. A 1 with no zeros before it changes nothing. Trace "001011": the 1s give 2, then 3, then 4. That's your answer, in O(n) time and O(1) space. The pitfall is simulating with string rebuilds, which is O(n^2), or returning only the zero count. StealthCoder is the hedge if the recurrence slips your mind mid-OA, but the whole thing fits in six lines.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Binary String Swap Time 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as time needed to rearrange a binary string. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Microsoft's OA.

Microsoft reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Binary String Swap Time FAQ

What's the trick in the Microsoft Binary String Swap Time problem?+

Stop simulating. Each 1 has to cross every zero to its left, and it can't move faster than the 1 ahead of it. Walk the string, count zeros, and for each 1 after a zero set time = max(time + 1, zeros). It runs in one pass.

How hard is this one really?+

Easy to medium. The brute force is trivial, so the difficulty is spotting the linear recurrence. If you've seen the zeros-count and max(time + 1, zeros) idea once, it's a five-minute problem. Without it, the simulation may time out on large inputs.

Why doesn't the answer just equal the number of zeros?+

Because blue uniforms block each other. In "0011" there are two zeros, but the second 1 has to wait a second behind the first, so the answer is 3. The time + 1 term in the recurrence captures that waiting.

Is this string-swap pattern still asked in 2026?+

It was reported for Microsoft in July 2026, so yes. Problems where simultaneous local swaps hide a counting formula show up regularly. Know the one-pass version and you can adapt it if the wording changes.

How do I prepare for this in 48 hours?+

Write the simulation first, then the one-pass version, and check both against "001011" returning 4. Test edge cases like all 1s, all 0s, and "1100", where the answer is 0. Then explain in one sentence why max(time + 1, zeros) works.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Microsoft.

OA at Microsoft?
Invisible during screen share
Get it