FastPrepOrganization Chart by Level

Organization Chart by Level

Microsoft logoMicrosoft● EasyNEW GRADONSITE INTERVIEW
Learn

Problem statement

An organization contains employees numbered from 0 through n - 1. The array manager describes a rooted tree: employee 0 is the root and has manager -1; for every other employee i, manager[i] is that employee's direct manager.

Return the complete organization chart one layer at a time from the root downward. Employees within each layer must appear in increasing employee-number order.

Function

organizationLevels(manager: int[]) → int[][]

Examples

Example 1

manager = [-1,0,0,1,1,2]return = [[0],[1,2],[3,4,5]]

Employee 0 is the root, employees 1 and 2 form the next layer, and their reports form the third.

Example 2

manager = [-1]return = [[0]]

A one-person organization has one layer.

Constraints

  • 1 <= manager.length <= 100000
  • manager[0] = -1
  • For 1 <= i < manager.length, 0 <= manager[i] < i

More Microsoft problems

See Microsoft hiring insights
public int[][] organizationLevels(int[] manager) {
  // Write your code here.
}
manager[-1,0,0,1,1,2]
expected[[0],[1,2],[3,4,5]]
Checking account…