← Questions
Remove one link and split a gold chain evenly
0
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 Apr 2026. It tests prefix sums and arrays.
An array weights describes a gold chain: each number is the weight of one link, in order. You must remove exactly one link, join the remaining links back together in the same order, and then cut the chain once into two non-empty pieces of equal total weight. Decide whether some choice of link and cut makes this possible.
Interface
Unless you agree on a different interface with the interviewer, assume:
can_split(weights: list[int]) -> bool- Follow-up:
all_splits(weights: list[int]) -> list[tuple[int, int]]— every pair(r, k)whereris the removed index in the original array andkis the cut index in the array after removal (the left piece is indices0..k). Sorted byr, thenk.
Example
can_split([1, 2, 1]) -> True # remove index 1: [1, 1], cut after index 0
can_split([1, 3, 2, 2]) -> False
all_splits([1, 2, 1]) -> [(1, 0)]
all_splits([1, 1, 1]) -> [(0, 0), (1, 0), (2, 0)]
all_splits([3, 1, 2, 4, 2]) -> [(3, 1)] # remove the 4: [3, 1 | 2, 2]
Constraints
-
0 <= len(weights) <= 2000; weights can be negative, in[-10^9, 10^9]. - An array with fewer than 3 links can never be split.
Clarifying questions
- Can weights be zero or negative?
- Must both pieces be non-empty?
- For the follow-up, can there be
O(n^2)answers? (Yes, with zeros or negatives.)
What a strong answer covers
- Prefix sums. With link
rremoved, the total isT - w[r], which must be even, and the left piece must weigh half of it. For a cut left ofrthe left weight is a prefix sum; for a cut right ofrit is a prefix sum minusw[r]. - The existence check in
O(n)overall with hash maps of prefix sums seen to the left and right ofr(positive weights also allow binary search), orO(n^2)with a direct scan, which fits the limits. - The follow-up enumerates pairs, so its cost is at least the output size; map each matching prefix back to the cut index
kcorrectly on each side ofr. - Tests: fewer than 3 links, all equal weights, negatives and zeros producing many answers.
Follow-ups
- Remove two links instead of one.
- Cut into three equal pieces.
- The chain is a loop (circular), so the first and last links are joined.
Where it's asked
- Google · Onsite coding · Last asked Apr 202645 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
prefix sums · 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.
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
