Binary Sorting Rounds After Flips
Reported by candidates from Visa's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Visa reported this one in August 2026, and the trap is in the definition. "Maximum over all remaining zeros of ones before it" sounds like you need a prefix structure and a scan after every flip. With 2 * 10^5 characters, that scan kills you. The problem is really an array problem with a two-pointer finish, and the answer collapses into a single formula once you spot it. If you've got a Visa OA coming and this shows up, you need the insight fast, not a Fenwick tree. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but the idea below is short enough to carry in your head.
The problem
You are given a binary string binary and an array flips. Process the flip indices in order. Each flip changes the character at that index from 0 to 1. After every flip, define the current sorting-round count as the maximum, over all remaining 0 characters, of the number of 1 characters before that zero. If no zero remains, the sorting-round count is 0. Return the sorting-round count after each flip. Function getSortingRounds(binary: String, flips: int[]) → int[] Examples Example 1 binary = "10100" flips = [1,3] return = [3,4] After flipping index 1, the remaining zeros are at indices 3 and 4, each with three ones before it. After flipping index 3, the final zero has four ones before it. Example 2 binary = "000" flips = [1,0,2] return = [1,2,0] The first flip leaves the last zero with one preceding one. The second leaves it with two preceding ones. The final flip removes the last zero, so the answer becomes 0. Constraints 1 <= binary.length <= 2 * 10^5. binary contains only 0 and 1. 1 <= flips.length <= binary.length. Every value in flips is a distinct 0-based index whose current character is 0.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Ones-before-index never decreases as the index grows. So the maximum is always reached at the last remaining zero. Call its position p. Every zero before p is still a zero, so the ones before p equal p minus the number of zeros before p. Zeros before p is zerosRemaining minus 1. Answer is p - (zerosRemaining - 1), or 0 if no zeros remain. Keep a boolean array of current characters. After each flip, decrement zerosRemaining, then slide p left while the character at p is 1. Zeros only disappear, so p never moves right and the whole thing is O(n). Check example 2: after flipping index 1 in "000", p is 2 with two zeros left, giving 1. The common pitfall is rescanning or building a segment tree you don't need. If your mind goes blank on the monotonic observation, StealthCoder can surface the formula during the live OA.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Binary Sorting Rounds After Flips 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Visa's OA.
Visa reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Binary Sorting Rounds After Flips FAQ
How hard is the Visa Binary Sorting Rounds problem really?+
Easy to code, medium to see. The implementation is about ten lines. The hard part is realizing the max always sits at the last remaining zero. Once you see that, no data structure is needed. Candidates who miss it reach for a Fenwick tree or brute force and burn time.
What's the trick to solve it in O(n)?+
Ones before a position only grow as you move right, so only the last zero matters. Its answer is p - (zerosRemaining - 1). Track zerosRemaining with a counter and move p leftward past any ones. Since p only moves left, total pointer movement is at most n.
Will brute force pass the constraints?+
No. Binary length goes up to 2 * 10^5 and flips can be as long as the string. Rescanning the whole string after each flip is O(n^2), around 4 * 10^10 operations in the worst case. You need the pointer approach or at least a Fenwick tree for O(n log n).
What edge cases should I test before submitting?+
Test the all-zeros string like "000", where the last flip leaves no zeros and you must return 0. Test a string with a leading one, like example 1. Test a single character. Also confirm p doesn't go below 0 when the last zero gets flipped.
How do I prepare for this in 48 hours?+
Practice spotting monotonic quantities that let you ignore most of the input. Hand-trace both examples with the formula p - (zerosRemaining - 1). Then code it once from scratch with a flipped array and a left-moving pointer. Skip heavy structures. This one rewards the observation, not machinery.