Skip to content

Instantly share code, notes, and snippets.

@SlimTim10
Forked from sastraxi/problem set.md
Created August 20, 2020 17:01
Show Gist options
  • Save SlimTim10/269d9319116b2ae00f384ab966c451f3 to your computer and use it in GitHub Desktop.
Save SlimTim10/269d9319116b2ae00f384ab966c451f3 to your computer and use it in GitHub Desktop.

Employee Hierarchies Problem Set

This problem set explores a number of questions related to a company and its employees; specifically, its hierarchy of management:

const employees = [
  { name: 'Alice',  manager: null,    title: 'CEO' },
  { name: 'Bob',    manager: 'Alice', title: 'Head of Marketing' },
  { name: 'Carol',  manager: 'Alice', title: 'Head of Engineering' },
  { name: 'Darcy',  manager: 'Carol', title: 'Systems Architect' },
  { name: 'Edwin',  manager: 'Bob',   title: 'SEO Specialist' },
  { name: 'Farah',  manager: 'Carol', title: 'Senior Developer' },
  { name: 'Georg',  manager: 'Carol', title: 'Junior Developer' }
];
  1. Write a function that takes the employee list and outputs the number of direct reports each employee has. For this dataset, the output would be:

    {
      'Alice': 2,
      'Bob': 1,
      'Carol': 3,
      'Darcy': 0,
      'Edwin': 0,
      'Farah': 0,
      'Georg': 0
    }
  2. Write a function that takes the employee list and outputs another array that contains an array of the names of an employee's direct reports instead of their manager's name, e.g.:

    [
      { name: 'Alice', reports: ['Bob', 'Carol'], title: 'CEO' },
      ...
    ]
  3. Modify the function from #2 to include indirect reports, too; i.e. if Alice is Bob's manager and Bob is Edwin's manager, Edwin is one of Alice's indirect reports.

  4. Write a function that takes the employee list and returns the depth of the employee hierarchy at the company. For this company, the depth is 3. A completely flat hierarchy (no managers) has a depth of 1.

  5. Suppose we were to change the dataset slightly and change Alice's manager to Darcy:

    { name: 'Alice', manager: 'Darcy', title: 'CEO (???)' }

    What would happen if you ran the code you wrote in #3 and #4? What is this type of error called? Write a function to detect if the dataset contains any of these problems, returning true if the dataset is invalid (contains any of these problems) and false otherwise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment