Fewest edges to collect all coins in a tree and return, with a collection radius of 2

Quick Overview

Find the fewest edge moves needed to collect every coin in a tree and come back to the starting node, when each stop collects coins up to two edges away. It tests repeated leaf pruning on trees, a cut-based lower bound argument, and linear-time graph processing.

Fewest edges to collect all coins in a tree and return, with a collection radius of 2

Company: Tower

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

There is an undirected, unrooted tree with `n` nodes labeled `0` to `n - 1`. You are given `edges`, a list of `n - 1` pairs where `edges[i] = [a, b]` means there is an edge between nodes `a` and `b`. You are also given an array `coins` of length `n`, where `coins[i]` is `1` if node `i` holds a coin and `0` otherwise. You first choose any node to start at. After that you may perform these operations any number of times, in any order: - Collect all the coins that are at a distance of at most `2` from the current node. - Move to an adjacent node. Return the minimum number of edges you need to traverse to collect all the coins and end back at the starting node. If you pass an edge several times, each pass counts. ### Function Signature ```python def collect_the_coins(coins: list[int], edges: list[list[int]]) -> int: ``` ### Rules - `n` is `len(coins)` and is not passed separately. - The distance between two nodes is the number of edges on the unique path between them. - Collecting coins costs nothing; only moves are counted. - Return `0` when no move is needed, for example when there are no coins or when one node is within distance `2` of every coin. ### Constraints - `1 <= n <= 3 * 10^4` - `coins[i]` is `0` or `1` - `len(edges) == n - 1` - Each edge `[a, b]` has `0 <= a, b < n` and `a != b` - `edges` forms a valid tree ### Examples ```text Input: coins = [1,0,0,0,0,1], edges = [[0,1],[1,2],[2,3],[3,4],[4,5]] Output: 2 ``` The tree is the path 0, 1, 2, 3, 4, 5 with coins at both ends. Start at node 2 and collect the coin at node 0 (distance 2), move to node 3 and collect the coin at node 5 (distance 2), then move back to node 2. No single node is within distance 2 of both ends, so fewer than 2 edges is impossible. ```text Input: coins = [0,0,0,1,1,0,0,1], edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[5,6],[5,7]] Output: 2 ``` Node 0 is joined to nodes 1 and 2, node 1 to nodes 3 and 4, node 2 to node 5, and node 5 to nodes 6 and 7. Coins sit at nodes 3, 4 and 7. Start at node 0 and collect the coins at 3 and 4 (distance 2 each), move to node 2 and collect the coin at 7 (distance 2 through node 5), then move back to node 0. Nodes 3 and 7 are 5 edges apart, so no single node can reach both. ```text Input: coins = [1,0,0,0,0,0,1], edges = [[0,1],[1,2],[2,3],[3,4],[4,5],[5,6]] Output: 4 ``` To collect the coin at node 0 you must stand on node 0, 1 or 2, and to collect the coin at node 6 you must stand on node 4, 5 or 6. The closest such pair is nodes 2 and 4, which are 2 edges apart, so the round trip costs 4 edges.

Overview: Find the fewest edge moves needed to collect every coin in a tree and come back to the starting node, when each stop collects coins up to two edges away. It tests repeated leaf pruning on trees, a cut-based lower bound argument, and linear-time graph processing.

Tower logo
Tower
Nov 25, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

There is an undirected, unrooted tree with n nodes labeled 0 to n - 1. You are given edges, a list of n - 1 pairs where edges[i] = [a, b] means there is an edge between nodes a and b. You are also given an array coins of length n, where coins[i] is 1 if node i holds a coin and 0 otherwise.

You first choose any node to start at. After that you may perform these operations any number of times, in any order:

  • Collect all the coins that are at a distance of at most 2 from the current node.
  • Move to an adjacent node.

Return the minimum number of edges you need to traverse to collect all the coins and end back at the starting node. If you pass an edge several times, each pass counts.

Function Signature

def collect_the_coins(coins: list[int], edges: list[list[int]]) -> int:

Rules

  • n is len(coins) and is not passed separately.
  • The distance between two nodes is the number of edges on the unique path between them.
  • Collecting coins costs nothing; only moves are counted.
  • Return 0 when no move is needed, for example when there are no coins or when one node is within distance 2 of every coin.

Constraints

  • 1 <= n <= 3 * 10^4
  • coins[i] is 0 or 1
  • len(edges) == n - 1
  • Each edge [a, b] has 0 <= a, b < n and a != b
  • edges forms a valid tree

Examples

Input: coins = [1,0,0,0,0,1], edges = [[0,1],[1,2],[2,3],[3,4],[4,5]]
Output: 2

The tree is the path 0, 1, 2, 3, 4, 5 with coins at both ends. Start at node 2 and collect the coin at node 0 (distance 2), move to node 3 and collect the coin at node 5 (distance 2), then move back to node 2. No single node is within distance 2 of both ends, so fewer than 2 edges is impossible.

Input: coins = [0,0,0,1,1,0,0,1], edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[5,6],[5,7]]
Output: 2

Node 0 is joined to nodes 1 and 2, node 1 to nodes 3 and 4, node 2 to node 5, and node 5 to nodes 6 and 7. Coins sit at nodes 3, 4 and 7. Start at node 0 and collect the coins at 3 and 4 (distance 2 each), move to node 2 and collect the coin at 7 (distance 2 through node 5), then move back to node 0. Nodes 3 and 7 are 5 edges apart, so no single node can reach both.

Input: coins = [1,0,0,0,0,0,1], edges = [[0,1],[1,2],[2,3],[3,4],[4,5],[5,6]]
Output: 4

To collect the coin at node 0 you must stand on node 0, 1 or 2, and to collect the coin at node 6 you must stand on node 4, 5 or 6. The closest such pair is nodes 2 and 4, which are 2 edges apart, so the round trip costs 4 edges.