Corporation Class: Set Teams and Managers, Check Reporting Lines
Company: Microsoft
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Technical Screen
Implement a `Corporation` class with three methods: `set_team`, `set_manager` and `check_reporting_line`. The report names only the class and its three methods, so the meaning of each method below is an assumption to confirm with the interviewer:
- `set_team(employee, team)` puts an employee on a team, moving them off any team they were on before.
- `set_manager(team, manager)` makes an employee the manager of a team, replacing any previous manager. Every member of the team reports to that manager. A manager is an employee too, and usually belongs to another team, which is how reporting lines climb toward the top of the company.
- `check_reporting_line(employee, manager)` returns `True` if `manager` is in `employee`'s chain of command: the manager of the employee's team, that manager's manager, and so on upward. Otherwise it returns `False`.
```text
set_team("E2", "platform")
set_team("E3", "platform")
set_manager("platform", "E1")
set_team("E1", "leadership")
set_manager("leadership", "E0")
check_reporting_line("E3", "E1") -> True E1 manages E3's team
check_reporting_line("E3", "E0") -> True E0 manages E1's team
check_reporting_line("E1", "E3") -> False
```
```hint What the calls build
Draw the structure these calls create as a graph. Decide what its nodes and edges are, and in which direction a reporting-line check has to walk.
```
```hint Updates that break the shape
Some calls would make a person their own manager, directly or through a longer loop. Decide which calls can create that, and where you detect it.
```
### Constraints and Clarifications
- Employees and teams are identified by strings. Under the reading above, an employee belongs to at most one team and a team has at most one manager.
- Not given: the number of employees, teams and calls, or which call is the most frequent.
### Clarifying Questions
- Does `set_manager` take a team, as assumed, or an individual employee? Can a team have several managers?
- Can an employee be on several teams at once? When an employee moves teams, should their reporting line change with them?
- Does an employee count as being in their own reporting line?
- What should happen on a call that would create a loop, such as making a team's member the manager of their own manager's team: reject it, or accept it and report it?
- Should unknown employees or teams raise an error, or simply give `False`?
- Which call dominates: frequent updates with rare checks, or a mostly static organization with many checks?
### What a Strong Answer Covers
- Agreed semantics for each method, including moves, replacements and self-reference
- A data model (employee to team, team to manager) that keeps every method cheap
- A correct upward walk for the check, with its cost stated in terms of the depth of the chain
- Loop prevention or detection, and the handling of unknown employees and teams
- Tests for direct, indirect, unrelated, moved and loop-creating cases
### Follow-up Questions
- The organization is large and checks vastly outnumber updates. How would you make a check close to constant time, and what would an update cost then?
- Add `lowest_common_manager(a, b)`. How would you implement it?
- An employee may now belong to several teams, and so have several managers. What changes in the check and in loop detection?
- How would you make the class safe to call from many threads at once?
Overview: An object-oriented coding question to implement a Corporation class with methods that put employees on teams, set each team's manager, and check whether one person is in another's reporting line. It tests data modeling, walking a management chain, preventing reporting loops, and query versus update trade-offs.