Split a two-colour necklace fairly with two cuts
I was asked this
No reports yet. Sign in to say where you were asked it.
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 positionsiandjof the circle. One person takes the arc fromiup to (not including)j, going forward and wrapping past the end if needed; the other takes the rest. Return any valid pair, orNoneif 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; onlyaandb. - 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 / 2beads, 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 / 2around the circle (overs + s, or with modular indices), keeping its count ofa. Moving the window one step changes the count by at most 1, and the window starting atn / 2has 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
kgem 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
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
