As reported: Rippling SDE-1 online assessment (Jan 2025), HackerRank, 60 minutes, proctored with screen share and webcam, question 1 of 2: a server network is a tree of
g_nodesservers numbered1tog_nodes, withg_nodes − 1linksg_from[i] — g_to[i]; sending data across one link takes 1 unit of time; find the maximum time needed to transfer data between any two servers.
This is LeetCode 1245 — Tree Diameter. Our write-up: /tree-diameter.
The network is given as an undirected tree on nodes 0 to n − 1 (n = edges.length + 1), where edges[i] = [a, b] is a link between servers a and b. Return the largest number of links on the path between any two servers — the tree's diameter.
Example 1
edges = [[0,1],[1,2]]
Output: 2
(The OA's g_nodes = 3, g_from = [1,2], g_to = [2,3], numbered from 0.)
Example 2
edges = [[0,4],[0,2],[0,1],[4,3]]
Output: 3
(The OA's g_nodes = 5, g_from = [1,1,1,5], g_to = [5,3,2,4]: the longest path is 4 → 5 → 1 → 3.)
Constraints: 1 ≤ n ≤ 10^4; the edges form a tree.
Code runner format: one JSON value per line: edges. Implement treeDiameter(edges).