Counting Binary Substrings
Reported by candidates from Canva's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Canva asked this in April 2024 and it's a classic string pattern problem that trips up candidates who overthink it. You're given a binary string and need to count substrings where the left half is all 0s and the right half is all 1s, in equal amounts, with no mixing between them. The trap is iterating naively when a linear pass with a counting trick solves it cleanly. StealthCoder can spot the pattern and output the solution if you blank on the grouping logic.
Pattern and pitfall
The core insight is to group consecutive identical characters, then iterate through adjacent groups. For each pair of consecutive groups (0s followed by 1s, or vice versa), the number of valid substrings is the minimum of the two group lengths. A candidate who tries to check every substring individually will timeout. The linear approach groups chars in one pass, then walks through groups in a second pass. Common mistake: forgetting that you only count transitions where opposite characters meet, and the count is bounded by the smaller group size.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Counting 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. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Canva's OA.
Canva reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Counting Binary Substrings FAQ
What exactly counts as a valid substring here?+
A substring where all left chars are the same (0 or 1), all right chars are the opposite, they're equal length, and there's exactly one transition point between them. Example: '0011' and '01' both count, but '0101' doesn't.
Will a brute-force check-every-substring approach pass?+
No. With string length up to 1000, O(n^2) or worse will timeout. You need the grouping trick: compress consecutive chars into group lengths, then iterate pairs. That's O(n).
How do I handle the group length logic?+
Store lengths of each consecutive group. For each adjacent pair of different characters, the number of valid substrings is min(length_of_group_1, length_of_group_2). Sum these across all transitions.
Is this pattern common in Canva or other OAs?+
Yes. Canva and similar companies use string grouping problems regularly. Once you see the grouping trick here, you'll recognize it in other problems asking about patterns within strings.
What should I focus on in the last 24 hours before the OA?+
Write out a few test cases by hand. Group '000111' and trace the logic. Make sure you understand why min(group_length_0, group_length_1) works. That clarity will stick under pressure.