Google questions

Gold Chain Split

GoogleOnsiteMedium

A gold chain is a row of links, and weights[i] is the weight of link i. Link indices are 0-based. GoldChain(weights) holds the chain.

python from typing import List

class GoldChain: def __init__(self, weights: List[int]) -> None: pass

def can_split(self) -> bool: raise NotImplementedError

Part 1: Can the chain be shared

Implement can_split(). It returns True if the chain can be shared like this:

  1. Remove exactly one link and join the loose ends, keeping the remaining links in their original order.
  2. Cut the remaining chain once, between two neighboring links, into two pieces that have the same total weight.

Both pieces must hold at least one link. Return False if no choice of removed link and cut position works.

  1. Example 1
    init
    [[1,2,3,4]]
    operations
    [["can_split",[]]]
    Output
    [true]

    Why: Removing the link of weight 2 (index 1) leaves 1, 3, 4. Cutting after the first two links gives pieces of weight 4 and 4.

Constraints

  • 1 <= len(weights) <= 10^5
  • 1 <= weights[i] <= 10^9
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question
Gold Chain Split: Google interview question | LoopCoach