FastPrepOrder Employees by a Reports-To Hierarchy

Order Employees by a Reports-To Hierarchy

Bloomberg LP logoBloomberg LP● MediumNEW GRADPHONE SCREEN
Learn

Problem statement

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.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public String[] orderEmployees(String[] names, String[] titles, String[][] reportsTo) {
  // Write your code here.
}
names["John","Sally","Sam","Drax","Bob","Daniel"]
titles["Manager","CTO","CEO","Engineer","CFO","Engineer"]
reportsTo[["CTO","CEO"],["Manager","CTO"],["Engineer","Manager"],["CFO","CEO"]]
expected["Drax", "Daniel", "John", "Sally", "Bob", "Sam"]
Checking account…