Chain of Command
Updated
Problem statement
An organization contains n people. Its structure is a tree in which each person is a node. Every node except the root has one parent, representing its upstream reporting line, and a node may have any number of children, representing the people who report to it.
Person 1 is the root. The array parent represents the tree: parent[i] is the parent of person i + 1, and parent[0] = -1 indicates that the root has no parent.
Directive propagation
A person may issue a directive to their reporting subtree. The issuing person receives it first, and the directive then propagates as follows:
- A person sends the directive to their direct children in ascending order of person index.
- Before sending it to the next child, the person waits until propagation through the current child's entire subtree is complete.
- Every child propagates the directive through its subtree using the same process.
- Propagation stops when every node in the issuing person's subtree has received the directive.
Queries
Each query is [startNode, k]. Determine the kth person to receive the directive when startNode issues it.
If k is greater than the number of people in that reporting subtree, return -1. Evaluate every query independently.
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 these direct reporting relationships:
- Person
1manages people2,3, and4. - Person
3manages people5and7. - Person
5manages people6and8. - Person
7manages person9.
The receive order is [1, 2, 3, 5, 6, 8, 7, 9, 4] from person 1, [3, 5, 6, 8, 7, 9] from person 3, [7, 9] from person 7, and [9] from person 9.
Processing the queries:
queries[0] = [1, 5]: the5th person is6.queries[1] = [7, 2]: the2nd person is9.queries[2] = [9, 2]: no2nd person exists, so the answer is-1.queries[3] = [3, 6]: the6th person is9.
Therefore, the returned array is [6, 9, -1, 9].
Example 2
parent = [-1, 1, 1, 2, 2]queries = [[1, 3], [2, 3]]return = [4, 5]The array parent represents these direct reporting relationships:
- Person
1manages people2and3. - Person
2manages people4and5.
The receive order is [1, 2, 4, 5, 3] from person 1 and [2, 4, 5] from person 2.
For queries[0] = [1, 3], the 3rd person is 4. For queries[1] = [2, 3], the 3rd person is 5.
Therefore, the returned array is [4, 5].
Constraints
2 <= n <= 10^51 <= parent[i] <= nfor all nodes except the root node, whereparent[0] = -1.1 <= q <= 2 * 10^51 <= queries[i][0] <= n1 <= queries[i][1] <= n
More IMC Trading Online Assessment questions
- Earliest Meeting Start on a 24-Hour ClockOA · Seen Nov 2026
- Grid Paths with Override PassesOA · Seen Oct 2026
- Weighted LFU CacheOA · Seen Sep 2026
- Rock JumpingOA · Seen Aug 2026
- Stack Batch RemovalOA · Seen Aug 2026
- Choose ContainersOA · Seen Aug 2026
- Sub-matrix SumsOA · Seen Jul 2026
- Sub-matrix SumsOA · Seen Jul 2026
- Can Every Package Fit in a BoxOA · Seen Jul 2026
- Minimum Space Wasted From PackagingOA · Seen Sep 2025
Similar questions at other companies
- Chain of CommandUber · OAUber interview questions
- Validate BST Node DescriptionsAirbnb · OAAirbnb interview questions
- Delete and Compact a Forest SubtreePinterest · PHONE SCREEN · ONSITE INTERVIEWPinterest interview questions
- Render a Comment HierarchyAmazon · ONSITE INTERVIEW
- Render a Stable Comment HierarchyBobyard · PHONE SCREEN
- Validate a Tree From Its Parent ArrayGoogle · PHONE SCREENGoogle interview questions
- Find Defenders Against Every Hostile MonsterConfluent · ONSITE INTERVIEWConfluent interview questions
- Recursive Display SortFigma · PHONE SCREEN · ONSITE INTERVIEWFigma interview questions
- Base Unit ConversionsAdobe · OAAdobe interview questions
- Count Prime Values in SubtreesVisa · OAVisa interview questions