Corporation Reporting Lines
Problem statement
Build a small model of a corporation. People belong to teams, and every team can have one manager. Process operations in order. Each operation is one of these:
["setTeam", employee, team]: putemployeeonteam. A person is on at most one team, so this replaces any team they were on before.["setManager", team, manager]: makemanagerthe manager ofteam, replacing any earlier manager. A manager is a person, may be on a team of their own, and may even be on the team they manage.["checkReportingLine", employee, manager]: answer whetheremployeereports tomanager, directly or through other managers.
The reporting line of a person is found like this. Start at the person. Find their team and that team's manager; that manager is the next person on the line. Then repeat from that manager. The line stops when the current person is on no team, when their team has no manager, or when the next person is someone already on the line, including the employee you started from.
employee reports to manager when manager appears on the line after employee. A person never reports to themself.
Team names and people names are separate: a team and a person may have the same name and are still different things.
Return the answers of all "checkReportingLine" operations, in order.
Function
processCorporationOperations(operations: String[][]) → boolean[]Examples
Example 1
operations = [["setTeam","ana","web"],["setTeam","raj","infra"],["setManager","web","raj"],["setManager","infra","lena"],["checkReportingLine","ana","raj"],["checkReportingLine","ana","lena"],["checkReportingLine","raj","ana"],["checkReportingLine","lena","raj"]]return = [true,true,false,false]ana is on team web, managed by raj. raj is on team infra, managed by lena. So the line of ana is ana, raj, lena, and ana reports to both raj and lena.
A line only goes upward, so raj does not report to ana. lena is on no team, so her line stops right away and she reports to nobody.
Example 2
operations = [["setTeam","kim","data"],["checkReportingLine","kim","omar"],["setManager","data","omar"],["checkReportingLine","kim","omar"],["setTeam","kim","apps"],["checkReportingLine","kim","omar"]]return = [false,true,false]The first check happens before team data has a manager, so the answer is false. After omar becomes its manager, kim reports to him. Then kim moves to team apps, which has no manager, so kim no longer reports to omar.
Example 3
operations = [["setTeam","ivy","core"],["setManager","core","ivy"],["checkReportingLine","ivy","ivy"],["setTeam","zed","core"],["checkReportingLine","zed","ivy"],["setTeam","ivy","ops"],["setManager","ops","zed"],["checkReportingLine","zed","ivy"],["checkReportingLine","ivy","zed"]]return = [false,true,true,true]ivy is on team core and also manages it. The next person on her line would be herself, so the line stops and ivy does not report to ivy. zed joins core, so zed reports to ivy.
Then ivy moves to team ops, managed by zed. Now zed reports to ivy and ivy reports to zed. The line of zed is zed, ivy, and then it stops because the next person would be zed again.
Constraints
1 <= operations.length <= 500- Each
operations[i]is["setTeam", employee, team],["setManager", team, manager], or["checkReportingLine", employee, manager]. - Every name has
1to10characters, all lowercase English letters or digits. - At least one operation is
"checkReportingLine".