Employee Hierarchy Cycle and Depth Analysis
Reported by candidates from Microsoft's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Microsoft OA reported in September 2026 looks like a simple tree depth question, then hides a trap: a cycle that never touches the root. If you only walk down from employee -1, you'll miss it and return a wrong answer. This is a graph problem on a parent array with n up to 2 * 10^5, so recursion depth and repeated work both matter. Read the statement twice, because the cycle clause is the whole problem. If you blank on the cycle detection during the live assessment, StealthCoder runs invisibly on your desktop as a safety net and gives you the solution in real time.
The problem
An organization has n employees labeled from 0 to n - 1. The array manager describes the direct manager relation: manager[i] = -1 means employee i is the root of the organization. Otherwise, manager[i] is the employee who directly manages employee i. Exactly one employee has manager -1. Every other entry is a valid employee index. The reported relations may still contain a directed cycle disconnected from the root. Return a two-element array: If any management cycle exists, return [1, -1]. Otherwise, return [0, maxDepth], where an employee's depth is the number of manager edges from the root to that employee and maxDepth is the greatest employee depth. Function analyzeHierarchy(manager: int[]) → int[] Examples Example 1 manager = [-1,0,0,1,1,3] return = [0,3] No cycle exists. Employee 5 is reached along 0 -> 1 -> 3 -> 5, so the maximum depth is 3. Example 2 manager = [1,2,0,-1] return = [1,-1] Employees 0, 1, and 2 form the management cycle 0 -> 1 -> 2 -> 0. Example 3 manager = [-1] return = [0,0] The only employee is the root, whose depth is 0. Constraints 1 <= manager.length <= 2 * 10^5 Exactly one entry of manager is -1. Every other entry is an employee index in [0, manager.length - 1].
Reported by candidates. Source: FastPrep
Pattern and pitfall
Each employee has exactly one manager, so this is a functional graph. The trick is to follow manager pointers upward with a state array: 0 unvisited, 1 in the current path, 2 done with a known depth. Walk up from each unvisited node, marking nodes as in-path. If you hit an in-path node, you found a cycle, so return [1, -1]. If you hit the root or a finished node, unwind the path and assign depths. That's O(n) total because every node is resolved once. The pitfall is a BFS or DFS from the root only. It never reaches a disconnected cycle, so those nodes stay unvisited and you'd report no cycle. Also avoid recursion at 2 * 10^5 depth, since a chain will blow the stack. Go iterative. A final check works too: if any node isn't reached from the root, a cycle exists. StealthCoder is the hedge if the iterative unwinding trips you up live.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Employee Hierarchy Cycle and Depth Analysis 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 Microsoft's OA.
Microsoft 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.
Employee Hierarchy Cycle and Depth Analysis FAQ
What's the trick in the Microsoft Employee Hierarchy Cycle and Depth question?+
The cycle can be disconnected from the root, so a traversal from the root alone misses it. Walk up the manager chain from every node with a visiting state, or check that all n nodes are reachable from the root. Either one catches the hidden cycle.
How hard is this OA really?+
Medium. The idea is short, but the edge cases bite. A cycle off the root, a single-node input like [-1], and deep chains at 2 * 10^5 all break lazy solutions. If you know three-state cycle detection, it's maybe 20 lines.
Should I use recursion or iteration?+
Iteration. A chain of 2 * 10^5 employees will overflow the stack in most languages with plain recursive DFS. Use an explicit path list while walking up the manager pointers, then assign depths while unwinding the stored path.
Can I solve it with BFS from the root?+
Yes. Build a children list, BFS from the root, and track the level. Count visited nodes. If the count is less than n, the leftover nodes sit in a cycle, so return [1, -1]. Otherwise the last level is maxDepth.
How do I prepare for this in 48 hours?+
Practice cycle detection in a functional graph and parent-array depth computation. Write it once iteratively, and test on [-1], a pure chain, and a 3-node cycle with a separate root. Those inputs cover nearly every failure mode.