← Questions

Remove one link and split a gold chain evenly

0
GoogleCodingLast asked Apr 2026

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) where r is the removed index in the original array and k is the cut index in the array after removal (the left piece is indices 0..k). Sorted by r, then k.

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 r removed, the total is T - w[r], which must be even, and the left piece must weigh half of it. For a cut left of r the left weight is a prefix sum; for a cut right of r it is a prefix sum minus w[r].
  • The existence check in O(n) overall with hash maps of prefix sums seen to the left and right of r (positive weights also allow binary search), or O(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 k correctly on each side of r.
  • 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

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