Validate a Hierarchy and Compute Propagation Time
MicrosoftSoftware Engineer · Technical ScreenLast updated
Tests graph validation and traversal by requiring candidates to verify whether directed relationships form a valid single-rooted directed tree and calculate the maximum propagation delay from the root.
Problem Statement
You are given an integer n, representing n servers labeled from 0 to n - 1, and a 2D integer array edges where edges[i] = [parent, child, delay]. This represents a direct communication link where parent transmits updates to child with a latency of delay milliseconds.
An alert system requires all servers to be organized in a valid rooted hierarchy (an arborescence):
- There must be exactly one designated root server (a server with no incoming edges).
- Every other server must have exactly one direct parent.
- All
nservers must be reachable from the root server (i.e., no cycles and no disconnected components).
If the configuration forms a valid hierarchy, return the maximum time it takes for an alert triggered at the root to propagate to all reachable servers. If the configuration is invalid, return -1.
Function Signature
def max_propagation_time(n: int, edges: list[list[int]]) -> int:
pass
Input & Output Format
- Input:
n: An integer representing the number of servers.edges: A list of lists, where each inner list contains three integers[u, v, w]denoting a directed edge fromutovwith weightw.
- Output:
- An integer representing the maximum root-to-leaf propagation time, or
-1if the network does not form a valid single-rooted tree.
- An integer representing the maximum root-to-leaf propagation time, or
Constraints
1 <= n <= 10^50 <= len(edges) <= 2 * 10^5edges[i] = [u, v, w]where0 <= u, v < nand0 <= w <= 10^6- There may be duplicate edges or self-loops in the input.
Examples
Example 1
Input: n = 4, edges = [[0, 1, 2], [0, 2, 5], [1, 3, 4]]
Output: 6
Explanation:
- Server 0 has in-degree 0 (root).
- Server 1 has parent 0, Server 2 has parent 0, Server 3 has parent 1.
- Propagation time to 1 is 2.
- Propagation time to 2 is 5.
- Propagation time to 3 is 2 + 4 = 6.
- All 4 servers are reachable. The maximum propagation time is 6.
Example 2
Input: n = 3, edges = [[0, 1, 3], [1, 2, 2], [2, 0, 1]]
Output: -1
Explanation:
There is a cycle (0 -> 1 -> 2 -> 0) and no node has in-degree 0. Return -1.
Example 3
Input: n = 3, edges = [[0, 1, 2], [2, 1, 3]]
Output: -1
Explanation:
Server 1 has two incoming edges (multiple parents), which violates tree properties. Return -1.
Hints
Hint 1
Check necessary tree structural properties first: the number of edges must be exactly n - 1, and every node except one must have an in-degree of 1.
Hint 2
Identify the unique candidate root (the only node with in-degree 0); if none or more than one exists, the graph cannot be a valid hierarchy.
Hint 3
Perform a BFS or DFS from the candidate root to verify that all n nodes are visited (detecting any cycles or disconnected nodes) while tracking the path distance to each node.
Follow-up questions
- How would you handle dynamic updates where edges are added or removed over time while querying propagation delays?
- If communication between nodes could happen in both directions with different upload/download delays, how would you find the optimal root to minimize maximum propagation time?
- How would you modify the solution to return the bottleneck path or handle memory constraints if n exceeds single-machine RAM?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.