Number of Balanced Strings
Reported by candidates from Moveworks's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Moveworks tested this in July 2024. You've got a string, and you need to count how many prefixes are balanced. A balanced string has equal counts of two characters, usually '0' and '1' or 'L' and 'R'. It sounds simple until you realize the trap: most candidates iterate left-to-right and lose track of what "balanced" means at each step. StealthCoder will catch the pattern in seconds if you blank during the live OA, but the trick is understanding that you're really just tracking a running difference.
Pattern and pitfall
The pattern is counting. You iterate through the string once, maintaining a counter that goes up for one character type and down for the other. Every time the counter hits zero, you've found a balanced prefix. The common mistake is overthinking it as a complex state machine when it's actually a single pass with a balance variable. The second trap is resetting your index or counter wrong, which breaks the prefix logic entirely. This problem tests whether you can reduce a conceptual problem (balance) into a single numeric variable and recognize when that variable signals completion. StealthCoder would show you this pattern instantly, but live, you're looking for the moment when your balance counter returns to zero.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Number of Balanced Strings 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 Moveworks's OA.
Moveworks 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.
Number of Balanced Strings FAQ
Is this really just a counter problem?+
Yes. One pass, one variable tracking the difference between character counts. When it hits zero, increment your result. The "balanced" condition is just balance counter equals zero. No fancy data structure needed.
What if the string doesn't start balanced?+
You still iterate from position 0. The first balanced prefix might not exist until much later, or might not exist at all. The problem definition tells you what the input guarantees. Count only those that do balance.
Do I need to handle both 'L'/'R' and '0'/'1'?+
The problem specifies which character pair. Read the exact problem statement carefully. It's one pairing per test case. Your solution increments the counter for one, decrements for the other, and counts zeros.
How do I verify my answer in 2 minutes before submission?+
Manually trace through a short example string. Mark each position where your counter is zero. Count them. Then run your code on the same string and compare. Mismatches mean your counter logic is backwards or your increment timing is off.
Is this still being asked by Moveworks in 2024?+
It appeared in July 2024, so yes. Companies cycle pattern variations. This specific problem or a close variant could appear again. The counting-with-balance pattern is foundational and recurs across multiple OAs.