Validate a Hierarchy and Compute Propagation Time

Quick Overview

Build a rooted hierarchy from relationship data, detect invalid cycles, and compute the longest root-to-node propagation distance.

Validate a Hierarchy and Compute Propagation Time

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

# Validate a Hierarchy and Compute Propagation Time Implement `hierarchy_propagation_time(manager: list[int], inform_time: list[int]) -> int`. Employee `i` reports to `manager[i]`. Exactly one root should have manager `-1`. When an employee receives a message, that employee takes `inform_time[i]` time units before all direct reports receive it. The root has the message at time zero. If the manager relation is not one rooted hierarchy because it has no root, multiple roots, an out-of-range parent, a self-parent, a cycle, or a node unreachable from the root, return `-1`. Otherwise, return the earliest time by which every employee has received the message. A leaf's own `inform_time` does not add time after that leaf receives the message. ## Valid Input Domain - The two arrays have equal length. - Every `inform_time[i]` is a nonnegative integer. ## Constraints - `1 <= manager.length <= 200,000` - `0 <= inform_time[i] <= 1,000,000` - The answer fits in a signed 64-bit integer. ## Public Examples ### Example 1 Input: `manager = [-1, 0, 0, 1], inform_time = [2, 3, 0, 0]` Output: `5` Employee `3` receives the message after the root's two units and employee `1`'s three units. ### Example 2 Input: `manager = [1, 0], inform_time = [1, 1]` Output: `-1` There is no root and the two employees form a cycle. ```hint Separate validity from timing Ensure that every node belongs to one acyclic rooted structure while accumulating the time along root-to-node paths. ```

Quick Answer: Build a rooted hierarchy from relationship data, detect invalid cycles, and compute the longest root-to-node propagation distance.

|Home/Coding & Algorithms/Microsoft
Microsoft logo
Microsoft
Sep 2, 2026
hardSoftware EngineerTechnical ScreenCoding & Algorithms
0
0

Validate a Hierarchy and Compute Propagation Time

Implement hierarchy_propagation_time(manager: list[int], inform_time: list[int]) -> int.

Employee i reports to manager[i]. Exactly one root should have manager -1. When an employee receives a message, that employee takes inform_time[i] time units before all direct reports receive it. The root has the message at time zero.

If the manager relation is not one rooted hierarchy because it has no root, multiple roots, an out-of-range parent, a self-parent, a cycle, or a node unreachable from the root, return -1. Otherwise, return the earliest time by which every employee has received the message. A leaf's own inform_time does not add time after that leaf receives the message.

Valid Input Domain

  • The two arrays have equal length.
  • Every inform_time[i] is a nonnegative integer.

Constraints

  • 1 <= manager.length <= 200,000
  • 0 <= inform_time[i] <= 1,000,000
  • The answer fits in a signed 64-bit integer.

Public Examples

Example 1

Input: manager = [-1, 0, 0, 1], inform_time = [2, 3, 0, 0]

Output: 5

Employee 3 receives the message after the root's two units and employee 1's three units.

Example 2

Input: manager = [1, 0], inform_time = [1, 1]

Output: -1

There is no root and the two employees form a cycle.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...