Chain of Command
Learn this problemProblem statement
An organization is made up of n people. The organizational structure can be represented as a tree, with each person representing a node in the tree. All but the root node has a single parent, representing the node's upstream reporting line. A node may have zero or more children, representing those people who report into them.
If person 1 is the root of the tree, we can represent this structure in an array parent, where parent[i] represents the parent of node i + 1. Note that parent[0] = -1 to indicate no parent.
Within this organization, directives may be issued from a person to all people reporting into them, including all descendants in the tree. These directives propagate as follows:
- A person sends the directive to its direct child nodes in ascending order of their indices.
- If a node has more than one child, the node waits for propagation to complete fully to each child's subtree before issuing the directive to the next child.
- Each child node propagates the directive to its subtree using the same process.
- The propagation stops when all nodes in the subtree have received the directive.
For a single directive, determine who was the kth person to receive it. This query can be modeled as a tuple [startNode, k], where startNode is the person issuing the directive.
If k is greater than the total number of people receiving the directive, including direct reports and descendants of their direct reports, return -1 to indicate that no kth person exists.
For a given organization, evaluate all queries independently and return an integer array whose element at each position is the answer to the corresponding query.
Function
chainOfCommand(parent: int[], queries: int[][]) → int[]Examples
Example 1
parent = [-1, 1, 1, 1, 3, 5, 3, 5, 7]queries = [[1, 5], [7, 2], [9, 2], [3, 6]]return = [6, 9, -1, 9]The array parent represents the tree shown in the source. If person 1 issues a directive, people receive it in the following order: [1, 2, 3, 5, 6, 8, 7, 9, 4].
If person 3 issues a directive, people receive it in the following order: [3, 5, 6, 8, 7, 9].
If person 7 issues a directive, people receive it in the following order: [7, 9].
If person 9 issues a directive, people receive it in the following order: [9].
Processing the queries:
queries[0] = [1, 5]: if person1issues a directive, the5th person receiving it would be6.queries[1] = [7, 2]: if person7issues a directive, the2nd person receiving it would be9.queries[2] = [9, 2]: if person9issues a directive, there is no2nd person to receive it.queries[3] = [3, 6]: if person3issues a directive, the6th person receiving it would be9.
Hence, the array returned is [6, 9, -1, 9].
Example 2
parent = [-1, 1, 1, 2, 2]queries = [[1, 3], [2, 3]]return = [4, 5]If person 1 issues a directive, people receive it in the following order: [1, 2, 4, 5, 3].
If person 2 issues a directive, people receive it in the following order: [2, 4, 5].
For queries[0] = [1, 3], the 3rd person to receive the directive is person 4. For queries[1] = [2, 3], the 3rd person to receive the directive is person 5.
Hence, the array returned is [4, 5].
Constraints
2 <= n <= 10^51 <= parent[i] <= nfor all nodes except the root node, whereparent[0] = -11 <= q <= 2 * 10^51 <= queries[i][0] <= n1 <= queries[i][1] <= n
More Uber problems
- Last Truck to Leave the LaneOA · Seen Jul 2026
- Jump Game with Prime-3 StepsOA · Seen Jun 2026
- Total Palindrome Substring CostOA · Seen Jun 2026
- Earliest Time All Users Are ConnectedPHONE SCREEN · Seen May 2026
- Tournament Rounds by RankPHONE SCREEN · Seen May 2026
- Farthest Seat AssignmentONSITE INTERVIEW · Seen May 2026
- Convex Function MinimizationPHONE SCREEN · Seen May 2026
- Maximal Square AreaONSITE INTERVIEW · Seen May 2026