Order Employees by a Reports-To Hierarchy
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Bloomberg reported this one in October 2019, and the detail that matters is in the statement: depth is the count of reporting links up to a top title, and ties keep their original order. That's a stable sort on a computed key. You build a title-to-manager map, compute each title's depth, then sort employees by depth descending. If your head goes blank on the depth part, StealthCoder runs invisibly during the live OA and gives you a working solution as a safety net.
The problem
names[i] has title titles[i]. Each reportsTo row is [subordinateTitle, managerTitle]. Define a title's depth as the number of reporting links to a top title. Return employee names sorted by descending title depth. Preserve original order among employees with equal depth. Function orderEmployees(names: String[], titles: String[], reportsTo: String[][]) → String[] Examples Example 1 names = ["John","Sally","Sam","Drax","Bob","Daniel"] titles = ["Manager","CTO","CEO","Engineer","CFO","Engineer"] reportsTo = [["CTO","CEO"],["Manager","CTO"],["Engineer","Manager"],["CFO","CEO"]] return = ["Drax","Daniel","John","Sally","Bob","Sam"] Engineer is deepest, then Manager, then CTO/CFO peers, then CEO. Constraints The title mapping is acyclic and each subordinate title has at most one manager. At most 10^5 employees.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to separate two jobs. First, turn reportsTo into a map from subordinate title to manager title. Each subordinate has at most one manager and there are no cycles, so you can walk up the chain from any title. Memoize the depth per title so you don't re-walk chains. With up to 10^5 employees, a naive walk on a deep chain can blow up, and recursion can overflow the stack, so go iterative or cache as you climb. Second, sort by negative depth with a stable sort. Most built-in sorts are stable, but check yours. The common pitfall is sorting by title name or losing input order on ties. Look at Example 1: CTO and CFO share depth 1, so John's and Sally's order follows the names array. Top titles get depth 0. Titles missing from reportsTo also count as depth 0. StealthCoder is your hedge if the memoized depth logic slips under pressure.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Order Employees by a Reports-To Hierarchy 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 StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg 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.
Order Employees by a Reports-To Hierarchy FAQ
What's the core trick in the Bloomberg employee ordering problem?+
Compute each title's depth once using a manager map and memoization, then do a stable sort of employees by depth descending. The depth is the number of links up to a top title. Stability handles the tie rule for you.
How hard is this one really?+
Easy to medium. There's no fancy algorithm. The risk is small details: depth for titles not in reportsTo, stack overflow on deep chains, and keeping original order on ties. If you handle those three, it's quick.
Do I need recursion or a graph traversal?+
Not a full graph traversal. Each title has at most one manager, so it's a chain walk up a parent map. Use a loop with a cache, or memoized recursion. The loop is safer with 10^5 employees and a possibly deep hierarchy.
How do I keep original order among equal depths?+
Use a stable sort keyed on negative depth, or sort indices with the index as a tiebreaker. Python's sorted is stable. In languages where it isn't guaranteed, sort by the pair (-depth, index) so ties resolve by position.
How do I prepare for this in 48 hours?+
Practice computing depth in a parent-pointer structure with memoization, and writing stable sorts with custom keys. Then test Example 1 by hand, including the CTO and CFO tie. Edge cases to run: a single employee, duplicate titles, and a title missing from reportsTo.