FastPrepBinary String Swap Time
Problems/Binary String Swap Time

Binary String Swap Time

Microsoft logoMicrosoft● MediumNEW GRADOA
QuestionFunctionExamples

Problem statement

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.

More Microsoft problems

  • Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
  • Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
  • Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
All Microsoft interview questionsSee Microsoft hiring insights
Saved
class Solution {
    public int getSwapTime(String color) {
        // Write your code here
    }

}
Checking account…