Count Binary Substrings
Reported by candidates from Microsoft's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Microsoft reportedly put Count Binary Substrings in front of candidates in September 2026, and the constraint is the first thing to read: s can be 100000 characters long. That kills any approach that checks every substring, since that's billions of pairs. The real answer is a single pass over run lengths, which is a counting problem hiding inside a string. If you've got the OA coming up, learn this shape now. And if you blank mid-assessment, StealthCoder runs invisibly on your desktop and can hand you the logic while you keep typing.
The problem
Given a binary string s, return the number of nonempty contiguous substrings that contain the same number of 0 and 1 characters, with every 0 and every 1 grouped consecutively inside the substring. Substrings are counted by their positions, so equal text occurring at different positions is counted more than once. Function countBinarySubstrings(s: String) → int Examples Example 1 s = "00110011" return = 6 The valid occurrences are 0011, 01, 1100, 10, 0011, and 01. Example 2 s = "10101" return = 4 Each adjacent pair forms one valid substring, and no longer substring has exactly two consecutive groups. Constraints 1 <= s.length <= 100000. Every character of s is 0 or 1.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: split the string into runs of identical characters. For every pair of adjacent runs with lengths a and b, you get exactly min(a, b) valid substrings. Sum that over all adjacent pairs. You don't even need an array of runs. Track the previous run length and the current run length as you scan. When the character changes, add min(prev, cur), then set prev to cur and reset cur to 1. Don't forget to add the final min after the loop ends, because the last pair never triggers a change. That's the most common bug. Another pitfall is deduplicating substrings, but the problem says to count by position, so don't. Example 2, 10101, has runs 1,1,1,1,1, giving four pairs and an answer of 4. It runs in O(n) time with O(1) space. If the logic slips away during the live OA, StealthCoder is your hedge, but the run-length idea is small enough to memorize tonight.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Count Binary Substrings 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 by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as count binary substrings. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Microsoft's OA.
Microsoft reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Count Binary Substrings FAQ
What's the trick to Count Binary Substrings?+
Group the string into runs of equal characters. For each neighboring pair of runs, the number of valid substrings is the smaller of the two lengths. Add those up across the whole string. It's one pass and no substring checking is needed.
Why does brute force fail here?+
The string can reach 100000 characters, so checking every substring means roughly five billion candidates, and validating each one costs more on top. You need a linear solution. The run-length approach does the job in a single scan.
Do duplicate substrings count separately?+
Yes. The problem counts by position, so 0011 appearing twice in 00110011 counts twice. Don't use a set. Just sum min(prev, cur) at each boundary between runs and you'll get the 6 expected in example 1.
What's the most common bug on this problem?+
Forgetting the last boundary. You add min(prev, cur) when the character changes, but the final run never sees a change after it. Add one more min(prev, cur) after the loop. Also remember cur resets to 1, not 0.
How do I prepare for this in 48 hours?+
Code it twice from memory, once with a runs array and once with two counters. Test on 00110011 (expect 6) and 10101 (expect 4). Then practice a few similar string-grouping problems. That's enough for this pattern before Microsoft's assessment.