13. Bureaucrats

Difficulty: 

We have access to the organizational chart of a company. We would like to know how many people are in the organization led by a specific person, or in other words, how many people (including that specific person) directly or indirectly report to him. Every person except for the head of the company reports to exactly one other person.

The data structure of the organizational chart consists of objects that contain a unique employee ID and direct reports. For example, if employee #1 is the boss of employees #2 and #3, and employee #3 is the boss of employee #4, we have:

{
  id: 1,
  reports: [
    {
      id: 2,
      reports: []
    },
    {
      id: 3,
      reports: [
        {
          id: 4,
          reports: []
        }
      ]
    }
  ]
}

In the above example, the size of the organization led by employee #1 is 4, whereas it's 2 for employee #3 and 1 for employee #4. Given an organization chart and an employee ID, return the size of the sub-organization, and return 0 if the employee doesn't exist.

Input:

countReports({
  "id": 1,
  "reports": [
    {
      "id": 2,
      "reports": []
    },
    {
      "id": 3,
      "reports": [
        {
          "id": 4,
          "reports": []
        }
      ]
    }
  ]
}, 1);

Output:

4

Time Limit:

500 (ms)

Hint (hover to see):

  • Hint #1



Results

ProblemsAcceptedWrongTimed OutError
140000

Input:

Expected Output:

Output: