Reported September 2026
Googleprefix sum

Balance a Two-Letter String with at Most Two Cuts

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

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

Google's September 2026 OA has a string problem that looks like a split puzzle and is really a prefix count problem. You get a string of only a and b, and you need one or two cuts so each recipient ends up with half the a's and half the b's. Length tops out at 400, which sounds friendly, but checking every pair of cuts while recounting each piece wastes effort and invites off-by-one bugs. The pattern is prefix counts plus a small scan. If you blank on the live OA, StealthCoder runs invisibly as a safety net and gives you the approach while you type.

The problem

A string s contains only a and b, with an even total count of each letter. Split it into contiguous pieces using at most two internal cuts, then give whole pieces to two recipients so each receives exactly half of all a characters and half of all b characters.
Return one cut boundary [c] when prefix s[0:c] is balanced against the suffix. Otherwise return two boundaries [l, r] when the middle piece s[l:r] contains both half-counts and the outer pieces go to the other recipient. Prefer one cut; within the chosen cut count, return lexicographically smallest boundaries. Return an empty array if no split exists.

Function
findBalanceCuts(s: String) → int[]

Examples
Example 1
s = "abba"
return = [2]
Case 1 exercises the documented deterministic contract.
Example 2
s = "aabb"
return = [1,3]
Case 2 exercises the documented deterministic contract.
Example 3
s = "aaaabbbb"
return = [2,6]
Case 3 exercises the documented deterministic contract.

Constraints
2 <= s.length <= 400.
s contains only a and b.
The total count of each letter is even.
Every cut is internal, so both recipients receive at least one character.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Build prefix counts of a and b so any substring count is a subtraction. Let A and B be the totals, with targets A/2 and B/2. First try one cut: for c from 1 to n-1, check whether prefix a equals A/2 and prefix b equals B/2. The first hit is the answer, since you prefer one cut and the smallest boundary. If none works, try two cuts: loop l from 1 and r from l+1 up to n-1, and check that the middle piece s[l:r] has exactly A/2 a's and B/2 b's. Loop in increasing l then r, so the first match is lexicographically smallest. At n=400 that's about 80,000 pairs with O(1) checks, which is trivial. The pitfalls: forgetting cuts must be internal, returning a two-cut answer when a one-cut exists, and recounting substrings each time. If the live OA rattles you, StealthCoder is the hedge for the exact loop order and edge cases.

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 Balance a Two-Letter String with at Most Two Cuts 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

⏵ The honest play

You've seen the question. Make sure you actually pass Google's OA.

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

Balance a Two-Letter String with at Most Two Cuts FAQ

What's the trick in this Google OA problem?+

Prefix counts of a and b. Every piece's counts become a subtraction, so you can test any cut or pair of cuts in constant time. Then it's just two ordered loops: one cut first, then two cuts, returning the first match found.

Do I need anything smarter than brute force?+

No. With length at most 400, checking all pairs of cuts is about 80,000 checks. That's fine if each check is O(1) via prefix sums. Brute force only hurts if you recount each substring from scratch inside the loops.

How do I get the lexicographically smallest answer?+

Iterate l from small to large, and for each l iterate r from l+1 upward. Return the first valid pair. For the one-cut case, iterate c upward and return immediately. Don't collect all answers and sort, it's unnecessary.

What edge cases break most solutions?+

Allowing a cut at 0 or n, which violates the internal-cut rule. Also returning two cuts when a single cut works, and forgetting the empty array when nothing matches. Test with aabb, which needs two cuts, and abba, which needs one.

How do I prepare for this in 48 hours?+

Practice prefix sum counting on strings and the pattern of trying fewer cuts first, then more. Write the one-cut and two-cut loops from memory twice. Check your examples by hand, since this problem's outputs are deterministic and easy to verify.

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

OA at Google?
Invisible during screen share
Get it