Chain of Command
Reported by candidates from Uber's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks a first attempt at Uber's Chain of Command, reported in July 2026, is simulating every query from scratch. With up to 2 * 10^5 queries on a tree of 10^5 nodes, that blows up fast. It's a tree traversal problem in disguise: each directive follows a preorder DFS with children visited in ascending order. If you've got the OA coming up, the goal is to see the one-pass trick before you start typing. And if you blank mid-assessment, StealthCoder sits invisibly on your screen as a safety net while you work it out.
The problem
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 person 1 issues a directive, the 5th person receiving it would be 6. queries[1] = [7, 2]: if person 7 issues a directive, the 2nd person receiving it would be 9. queries[2] = [9, 2]: if person 9 issues a directive, there is no 2nd person to receive it. queries[3] = [3, 6]: if person 3 issues a directive, the 6th person receiving it would be 9. 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^5 1 <= parent[i] <= n for all nodes except the root node, where parent[0] = -1 1 <= q <= 2 * 10^5 1 <= queries[i][0] <= n 1 <= queries[i][1] <= n
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is one preorder DFS from the root, children sorted ascending, recording each node's entry index tin[v] and subtree size sz[v]. The order for any startNode is exactly the contiguous slice of the global preorder starting at tin[start] with length sz[start]. So the kth person is order[tin[start] + k - 1], valid only if k <= sz[start], otherwise -1. Every query becomes O(1) after O(n) preprocessing. Pitfalls: recursing on 10^5 nodes can overflow the stack in some languages, so go iterative. Build the children lists by looping i from 2 to n, which already gives ascending order, no sorting needed. Watch the off-by-one between 0-indexed parent and 1-indexed people. Also don't assume the root is always person 1 without using parent[0] = -1. If the indexing trips you up live, StealthCoder is the hedge that gives you a clean iterative version.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Chain of Command cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Uber's OA.
Uber reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Chain of Command FAQ
How hard is Chain of Command really?+
Medium. The tree part is easy. The catch is realizing subtree order is a contiguous slice of one global preorder. Once you see that, it's about 25 lines. Brute-force DFS per query is what fails on the large constraints.
What's the trick to answer queries fast?+
Run one DFS and store the preorder array, each node's start position, and its subtree size. For query [s, k], return order[pos[s] + k - 1] if k <= size[s], else -1. Each query is constant time.
Do I need to sort children?+
No. If you build child lists by iterating nodes in increasing index, each list is already ascending. Sorting would just add needless cost. Just append i+1 to the list of parent[i] as you loop.
Should I use recursion or iteration for the DFS?+
Go iterative with an explicit stack. Depth can hit 10^5 on a chain-shaped tree, which risks a stack overflow. Push children in reverse order so the smallest index pops first, and compute subtree sizes in a reverse pass.
How do I prepare for this in 48 hours?+
Practice preorder with entry index and subtree size, the Euler tour idea. Write it once iteratively. Then test on both examples, a single-chain tree, and a query where k exceeds the subtree size to confirm the -1 case.