Skip to content

Instantly share code, notes, and snippets.

@wise-introvert
Last active April 3, 2024 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wise-introvert/fd0f5ff14cad1a3a5376400a2d712f0c to your computer and use it in GitHub Desktop.
Save wise-introvert/fd0f5ff14cad1a3a5376400a2d712f0c to your computer and use it in GitHub Desktop.

Question

Imagine a sophisticated hierarchical data structure in JavaScript that models an organization's intricacies, encompassing details such as employee ID, name, position, department, and subordinates. Your task is to design a function, getEmployeesByPosition(organization, position) ( starting code is provided below ), which takes this organizational data structure as input along with a position ( e.g “Software Engineer” ) and efficiently produces an array containing information about all employees holding the given position within the organization. Your implementation should seamlessly navigate the complexity of the hierarchical data and deliver a clear and concise result. Either use JSPlayground as your environment or any online editor that you're comfortable with.

Organization object:

const organization = {
  id: 1,
  name: "Company",
  type: "Organization",
  employees: [
    {
      id: 2,
      name: "John Doe",
      position: "Manager",
      department: "Operations",
      subordinates: [
        {
          id: 3,
          name: "Alice Smith",
          position: "Team Lead",
          department: "Operations",
          subordinates: [],
        },
        {
          id: 4,
          name: "Bob Johnson",
          position: "Senior Developer",
          department: "Engineering",
          subordinates: [
            {
              id: 5,
              name: "Charlie Brown",
              position: "Software Engineer",
              department: "Engineering",
              subordinates: [
                {
                  id: 14,
                  name: "Jason Clive",
                  position: "Software Engineer",
                  department: "Engineering",
                  subordinates: [],
                },
              ],
            },
          ],
        },
      ],
    },
    {
      id: 6,
      name: "Jane Miller",
      position: "Software Engineer",
      department: "Human Resources",
      subordinates: [],
    },
  ],
};

Starting code

/**
 * Retrieves an array containing information about all employees holding the 
 * given position within the organization's hierarchical data structure.
 *
 * @param {Object} organization - The hierarchical data structure representing the organization.
 * @param {string} position - The position for which employees' information is to be 
 *                            retrieved (e.g., "Software Engineer").
 * @returns {Array<Object>} - An array containing information about employees holding the specified position.
 *   Each element in the array is an object with properties like employee ID, name, position, and department.
 *   The array is structured to navigate through the organizational hierarchy.
 */
function getEmployeesByPosition(organization, position) {
  // ...your code...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment