Problem · Tree

Maximum Data Transfer Time

Learn this problem
MediumMicrosoftINTERNOA
See Microsoft hiring insights

Problem statement

A server network is represented as a tree of g_nodes servers numbered from 1 to g_nodes. The network contains g_nodes - 1 edges, where the ith edge connects servers g_from[i] and g_to[i].

Transferring data across one edge takes 1 unit of time. Return the maximum time required to transfer data between any two servers in the network.

Function

getMaxTime(g_nodes: int, g_from: int[], g_to: int[]) → int

Examples

Example 1

g_nodes = 3g_from = [1,2]g_to = [2,3]return = 2

The longest transfer path is from server 1 to server 3. It crosses two edges, so the maximum transfer time is 2.

Constraints

  • 1 ≤ g_nodes ≤ 5 * 10^4
  • 1 ≤ g_from[i], g_to[i] ≤ g_nodes

More Microsoft problems

drafts saved locally
public int getMaxTime(int g_nodes, int[] g_from, int[] g_to) {
  // write your code here
}
g_nodes3
g_from[1,2]
g_to[2,3]
expected2
checking account