Skip to content

Instantly share code, notes, and snippets.

@mwicks7
Last active April 21, 2023 12:05
Show Gist options
  • Save mwicks7/d0163ff1a1476748c5d19ad24e32d4f7 to your computer and use it in GitHub Desktop.
Save mwicks7/d0163ff1a1476748c5d19ad24e32d4f7 to your computer and use it in GitHub Desktop.
Matt Wicks' answer to Sleep Number interview exercise
/**
* Given an array of Person objects, returns the root PersonTreeNode (the CEO).
* @param {Person[]} employees - An array of Person objects representing all the employees of the company.
* @returns {PersonTreeNode} The CEO of the organization.
*/
function generateTree(employees) {
var ceo = null;
const employeeNodes = employees.map(emp => PersonTreeNode(emp));
const getNodeById = (id) => employeeNodes.filter(en => en.person.id === id)[0];
employeeNodes.forEach(en => {
if (en.person.manager === null) {
ceo = en;
} else {
const manager = getNodeById(en.person.manager.id);
const directReporter = en;
manager.directReports.push(directReporter);
}
});
return ceo;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment