Reported August 2026
IMCtree

Chain of Command

Reported by candidates from IMC's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live IMC OA. Under 2s to a working solution.
Founder's read

The IMC OA reported in August 2026 is called Chain of Command, and it hinges on one data structure: a preorder traversal array of the tree. If you're taking it in the next few days, here's the gist. Each query asks for the kth person to get a directive from a given node, and there can be 2 * 10^5 of them on a tree of 10^5 nodes. Walking the subtree per query will time out. Flatten the tree once, then answer every query with an index lookup. If you blank on the setup during the live OA, StealthCoder can sit invisibly as a safety net.

The problem

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 1 manages people 2, 3, and 4.
Person 3 manages people 5 and 7.
Person 5 manages people 6 and 8.
Person 7 manages person 9.
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]: the 5th person is 6.
queries[1] = [7, 2]: the 2nd person is 9.
queries[2] = [9, 2]: no 2nd person exists, so the answer is -1.
queries[3] = [3, 6]: the 6th person is 9.
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 1 manages people 2 and 3.
Person 2 manages people 4 and 5.
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^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 the Euler tour. Build child lists, sorted ascending by index. Run one iterative DFS from person 1 and record the preorder sequence, plus tin[v] (position of v) and size[v] (subtree size). The receive order from any startNode is exactly the contiguous slice of preorder starting at tin[startNode] with length size[startNode]. So the answer to [s, k] is order[tin[s] + k - 1] if k <= size[s], else -1. Every query is O(1) after O(n) setup. Pitfalls: recursive DFS blows the stack at n = 10^5 in many languages, so go iterative. Children must be visited in ascending order, and the parent array is offset by one, since parent[i] belongs to person i + 1. Also watch that k can be as large as n. If the assessment clock is ticking and the index math slips, StealthCoder is the hedge that hands you the flatten-and-slice solution live.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

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. If you're reading this with an OA window open, you're who this was built for.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass IMC's OA.

IMC reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Chain of Command FAQ

What's the trick in Chain of Command?+

Flatten the tree once with a DFS preorder. Every subtree becomes a contiguous slice of that array. Store each node's start index and subtree size, then each query is a single array lookup plus a bounds check against the size.

How hard is this IMC problem really?+

Medium. The idea is short once you see the subtree-as-slice property. Most of the difficulty is the constraints. Per-query traversal is O(n * q) and fails at 10^5 nodes and 2 * 10^5 queries, so you need the precomputed approach.

Do I need recursion or iteration for the DFS?+

Use iteration with an explicit stack. A chain-shaped tree of 10^5 nodes can overflow the call stack in many languages. Push children in reverse sorted order so the smallest index pops first and preorder stays ascending.

How do I handle the parent array indexing?+

parent[i] is the parent of person i + 1, and parent[0] = -1 marks the root, person 1. Loop i from 1 to n - 1, append person i + 1 to parent[i]'s child list. Building in increasing i keeps children already sorted ascending.

How do I prepare for this in 48 hours?+

Practice writing an iterative preorder that records tin and subtree size, then answering range lookups from it. Test on Example 1, where the order from person 1 is [1, 2, 3, 5, 6, 8, 7, 9, 4]. Check the k greater than size case returns -1.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with IMC.

OA at IMC?
Invisible during screen share
Get it