Top-Down Employee Reporting Forest
Reported by candidates from Okta's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks a first attempt on this Okta question, reported in September 2026, is writing a clean recursive DFS and watching it crash on a long reporting chain. The task: take [employee, manager] rows, build a forest, and print a preorder walk as depth:name with roots and reports sorted lexicographically. It's a tree traversal wrapped in a hash map and a sort. With up to 100000 rows, one deep chain can blow the stack. If you blank during the live OA, StealthCoder runs invisibly on your desktop as a safety net and gives you the iterative version.
The problem
Each row of employeeManager is [employee, manager]. An empty manager string marks a root. The rows describe a valid reporting forest: every employee appears once, every non-empty manager is also an employee, and there are no cycles. Return the forest in deterministic top-down preorder. Visit roots in lexicographic order and visit each manager's direct reports in lexicographic order. Encode each visited employee as depth:name, where roots have depth 0. Function topDownReportingForest(employeeManager: String[][]) → String[] Examples Example 1 employeeManager = [["alice",""],["bob","alice"],["cara","alice"],["dan","bob"]] return = ["0:alice","1:bob","2:dan","1:cara"] A preorder walk visits Alice, Bob's subtree, and then Cara. Example 2 employeeManager = [["zane",""],["amy",""],["lee","zane"]] return = ["0:amy","0:zane","1:lee"] The two roots are ordered lexicographically before their subtrees are traversed. Example 3 employeeManager = [["solo",""]] return = ["0:solo"] A one-person organization is a one-node forest. Constraints 1 <= employeeManager.length <= 100000. Every row has exactly two strings. Employee names are unique non-empty lowercase identifiers. The manager is either empty or names another employee. The relationships form a forest.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Build a map from manager to a list of direct reports, and collect roots where the manager string is empty. Sort the roots and sort each report list once. Then run a preorder traversal, emitting depth + ":" + name as you pop each node. The pitfall is recursion depth. A 100000-person chain means 100000 stack frames, which fails in most languages. Use an explicit stack instead. Push children in reverse sorted order so the smallest name pops first, and store (name, depth) pairs on the stack. The other trap is sorting at the wrong time. Sort each list once after building the map, not during traversal. Total cost is O(n log n) for the sorting and O(n) for the walk. Check Example 2 by hand, since the roots come out as amy then zane regardless of input order. If the assessment clock is ticking and the iterative stack won't come together, StealthCoder is the hedge that reads the problem and hands you working code.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Top-Down Employee Reporting Forest 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 by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Okta's OA.
Okta reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Top-Down Employee Reporting Forest FAQ
How hard is the Okta Top-Down Employee Reporting Forest problem really?+
Easy to medium. The idea is simple: group by manager, sort, preorder walk. It gets harder only because of the input size. A recursive solution that looks right can still fail on a deep chain, so the real difficulty is handling depth safely.
What's the trick to avoid stack overflow here?+
Use an explicit stack instead of recursion. Push (name, depth) pairs, and push each manager's sorted children in reverse order so the lexicographically smallest pops first. That gives the exact preorder the problem wants with no recursion limit.
How do I find the roots in the input?+
Any row whose manager string is empty is a root. Collect those names, sort them lexicographically, and start your traversal from them in that order. The constraints guarantee a valid forest, so you don't need cycle checks or missing-manager handling.
What's the time complexity I should aim for?+
O(n log n) overall. Building the map is O(n), sorting the roots and each report list costs O(n log n) in total, and the traversal visits each employee once. Sort each list a single time, not repeatedly during the walk.
How do I prepare for this in 48 hours?+
Practice one forest-to-preorder problem with an iterative stack and a sorted adjacency map. Hand-check the examples, especially the one with two roots given out of order. Know how to carry depth alongside each node. That covers almost everything this question tests.