As reported: Goldman Sachs CoderPad rounds (2025–2026), a frequent second question: a forest (one or more disconnected trees) is given as a map
immediateParentof child → parent; return the id of the root of the largest tree, and if several trees are equally large, the smallest root id (Analyst; solved with hints). Other reports: a list of (child, parent) pairs where ties went to the higher id (Associate, 2026); return the size of the largest tree / largest connected component (USA, 2026; Associate SDE-II, 2025).
A forest is described by edges, a list of [child, parent] pairs: each child has exactly one parent, a parent may have many children, and there are no cycles. Every id that appears is a node; a node that is never a child is a root.
Return the id of the root of the tree with the most nodes. If several trees tie, return the smallest root id.
Example 1
edges = [[1,2],[3,4]]
-> 2 two trees of 2 nodes (roots 2 and 4); the smaller root wins
Example 2
edges = [[1,2],[3,1],[4,1],[5,6]]
-> 2 tree {2,1,3,4} has 4 nodes
Constraints: 1 ≤ edges.length ≤ 10^5; ids are positive integers up to 10^9; the input is a valid forest.
Code runner format: one JSON value per line: edges. Implement largestTree(edges); the harness prints the root id.