Reported May 2026
Salesforcestring

Time Needed to Rearrange a Binary String

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

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

The classic way to fail the Salesforce "Time Needed to Rearrange a Binary String" question, reported in May 2026, is to simulate it literally and then wonder why your logic or your runtime falls apart. Every second, each 01 flips to 10, and you repeat until none are left. It looks like a string problem, but the real pattern is a counting trick over one pass. If you've got an OA invite and 48 hours, learn the shortcut below. StealthCoder sits invisibly on your screen as a safety net if you blank during the live assessment.

The problem

You are given a binary string s. During each second, replace every occurrence of 01 with 10 simultaneously.
Repeat the process until s contains no occurrence of 01. Return the number of seconds required.

Function
secondsToRearrange(s: String) → int

Examples
Example 1
s = "0110101"
return = 4
The successive strings are 1011010, 1101100, 1110100, and 1111000. After 4 seconds, no 01 remains.
Example 2
s = "11100"
return = 0
The string already contains no 01, so the process requires 0 seconds.

Constraints
1 <= s.length <= 1000
Every character of s is either 0 or 1.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: every 1 needs to move left past every 0 ahead of it. Scan left to right and count zeros seen so far. When you hit a 1 and zeros > 0, the answer becomes max(answer + 1, zeros). The +1 covers a 1 stuck behind another 1 that is still moving, and zeros covers a free-running 1 that just needs to pass all zeros. Check it on 0110101: the first 1 gives 1, the second gives max(2,1)=2, the next 1 (zeros=2) gives max(3,2)=3, and the last 1 (zeros=3) gives max(4,3)=4. That matches. The common pitfall is replacing 01 with 10 one pair at a time in a single pass, which cascades and breaks the simultaneous rule. The naive simulation also works at n=1000, but the O(n) scan is cleaner. If your head goes blank mid-assessment, StealthCoder is the hedge that reads the problem and hands you this.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Time Needed to Rearrange a 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. If you're reading this with an OA window open, you're who this was built for.

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 Salesforce's OA.

Salesforce 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.

Time Needed to Rearrange a Binary String FAQ

What's the trick for the Salesforce binary string rearrange problem?+

Don't simulate. Track zeros seen so far while scanning left to right. For each 1 that has at least one zero before it, set time = max(time + 1, zeros). The final time is the answer. It runs in one pass with constant extra space.

Is brute-force simulation fast enough here?+

With length up to 1000, simulating second by second is at most about 1000 rounds of 1000 characters, so it likely passes. The risk is bugs. You must build the next string from the old one, not mutate in place, or the simultaneous swaps break. The linear method is safer.

Why does the answer use max(time + 1, zeros)?+

A 1 needs at least as many seconds as zeros ahead of it. But if the 1 before it finished at time t, this one can't finish before t + 1 because it gets blocked. Taking the max of both constraints covers each case.

What edge cases should I test?+

Test a string with no 01 like 11100, which returns 0. Test all zeros, all ones, and a single character. Test 0110101 and expect 4. Also try 000111, where every 1 must cross three zeros, and check your formula gives 5.

How do I prepare for this in 48 hours?+

Write the simulation first so you trust the behavior, then write the one-pass version and compare outputs on random binary strings. Once they match, you understand it. Then do a few similar string and counting problems so the pattern recognition is fast under time pressure.

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

OA at Salesforce?
Invisible during screen share
Get it