← Questions

Split a two-colour necklace fairly with two cuts

0
GoogleCodingLast asked Sep 2025

A coding interview question asked in Google's onsite coding round, last asked Sep 2025. It tests sliding window and arrays.

A necklace is a circle of beads, each a or b, given as a string read around the circle from some point. You may make at most two cuts. Can the pieces be shared between two people so each gets the same number of a beads and the same number of b beads? If so, return where to cut; otherwise say it is impossible. State the conditions that make it possible, then solve it in O(n).

Interface

Unless you agree on a different interface with the interviewer, assume:

  • fair_cuts(s: str) -> tuple[int, int] | None — cuts before positions i and j of the circle. One person takes the arc from i up to (not including) j, going forward and wrapping past the end if needed; the other takes the rest. Return any valid pair, or None if no split works.

Example

fair_cuts("aabb")     ->  (1, 3)    # arc "ab", rest "ba" (positions 3 and 0)
fair_cuts("abab")     ->  (0, 2)
fair_cuts("aaaabb")   ->  (2, 5)    # arc "aab", rest "baa"
fair_cuts("aab")      ->  None      # three a's can't be shared equally
fair_cuts("abbaab")   ->  None      # three a's

Constraints

  • 1 <= len(s) <= 10^5; only a and b.
  • O(n) time, O(1) or O(n) extra space.

Clarifying questions

  • Is the string a circle (the last bead touches the first) or a straight line?
  • Does each person get one contiguous piece, or can a person get several?
  • If several cuts work, is any one fine?
  • Does zero cuts count when one person gets nothing (only if the necklace is empty)?

What a strong answer covers

  • The necessary condition: both counts are even. Then each share has n / 2 beads, so one person's arc has exactly half the length.
  • Brute force first: try every start and length, O(n^2) or worse.
  • O(n): slide a window of length n / 2 around the circle (over s + s, or with modular indices), keeping its count of a. Moving the window one step changes the count by at most 1, and the window starting at n / 2 has the complementary count, so the count passes through exactly half: a split always exists when both counts are even.
  • That argument as the correctness proof (a discrete intermediate value theorem), and why it guarantees an answer rather than just finding one when lucky.
  • Tests: odd counts, all one colour, alternating beads, a wrap-around answer.

Follow-ups

  • Three kinds of gems, each with an even count, and up to three cuts. What is the generalised algorithm, and why might three cuts be needed?
  • With k gem types, how many cuts suffice (the necklace splitting theorem), and how does the search scale?
  • The necklace is a straight string instead of a circle.

Where it's asked

  • Google · Onsite coding · Last asked Sep 202545 min · Usually two rounds, mostly in person: whiteboard plus a Chromebook with Google Docs · No AI (a pilot code-comprehension round lets candidates use Gemini)

What it tests

sliding window · arrays

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding