Validate employee records form a single-rooted reporting tree
An HR tool imports reporting lines from a spreadsheet, one record per employee. Before it renders the org chart, it must check that the records really form a single tree with one person at the top. When they do not, the import screen shows one clear reason.
Implement findOrgChartError(employees). It returns null when the records form a valid org chart, or a string naming the problem.
findOrgChartError(employees) // => null | 'duplicate-id' | 'unknown-manager' | 'no-root' | 'multiple-roots' | 'cycle'
employees is an array of records { id, managerId }. id is a string. managerId is a string, or null for someone with no manager.id is unique.managerId is the id of an employee in the list.managerId: null.When several problems exist, return the first one in this list that applies:
'duplicate-id': two records have the same id.'unknown-manager': a managerId does not match any id.'no-root': no record has managerId: null. An empty list is reported this way too.'multiple-roots': more than one record has managerId: null.'cycle': some employees never reach the root because their reporting lines loop, including someone who manages themselves.employees.findOrgChartError([
{ id: 'cto', managerId: 'ceo' },
{ id: 'ceo', managerId: null },
{ id: 'dev1', managerId: 'cto' },
{ id: 'cfo', managerId: 'ceo' },
]); // => null
findOrgChartError([
{ id: 'ceo', managerId: null },
{ id: 'a', managerId: 'b' },
{ id: 'b', managerId: 'a' },
]); // => 'cycle' (a and b report to each other)
findOrgChartError([
{ id: 'ceo', managerId: null },
{ id: 'coo', managerId: null },
]); // => 'multiple-roots'
findOrgChartError([
{ id: 'ceo', managerId: null },
{ id: 'pm', managerId: 'vp' },
]); // => 'unknown-manager'
findOrgChartError([]); // => 'no-root'