Last active
April 21, 2023 12:05
-
-
Save mwicks7/d0163ff1a1476748c5d19ad24e32d4f7 to your computer and use it in GitHub Desktop.
Matt Wicks' answer to Sleep Number interview exercise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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