FastPrepBalance a Two-Letter String with at Most Two Cuts
Problems/Balance a Two-Letter String with at Most Two Cuts

Balance a Two-Letter String with at Most Two Cuts

Google logoGoogle● MediumFULLTIMEONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The reported task identifies this algorithmic exercise. The executable interface, bounds, deterministic tie rules, and examples are the documented practice contract.

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.

More Google problems

  • Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
  • Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
  • 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int[] findBalanceCuts(String s) {
        // Write your code here.
    }

}
Checking account…