Skip to the code
cf.
+10 XP

#323 Org Chart Rows

easy

An HR dashboard draws the company's org chart one row per management level. The chart comes from the API as nested JSON: each person is { "name": ..., "reports": [...] }, where reports lists their direct reports in display order. reports may be missing or empty.

Write chartRows(chart) that returns an array of rows. Row 0 holds the top person's name, row 1 the names of their direct reports, and so on. Inside a row, names appear left to right: all of the first person's reports, then the next person's, and so on. If chart is null, return [].

Example 1

Input: chart = {"name":"Ana","reports":[{"name":"Bo","reports":[{"name":"Dee"}]},{"name":"Cy","reports":[{"name":"Eli"},{"name":"Fay"}]}]}
Output: [["Ana"],["Bo","Cy"],["Dee","Eli","Fay"]]

Bo's report comes before Cy's reports, because Bo is to the left of Cy.

Example 2

Input: chart = {"name":"Solo","reports":[]}
Output: [["Solo"]]

One person, one row.

Constraints

  • 0 <= number of people <= 10000
  • Names are non-empty strings.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).