KatInterview

Stop Listing and Indented Reporting Tree from Raw Inputs

Trees & GraphsAuto-graded · 15 test cases
MediumAuto-gradedTrees & Graphsjsontreedfs

Last updated

Parse a JSON string into formatted stop lines and turn employee rows into a sorted, indented reporting-tree outline using an adjacency list and iterative DFS.

Write a function that handles two small, independent jobs and returns both results in one dictionary.

Part A: Stop lines

payload_text is a string containing valid JSON. It is an object that may contain a key "stops" holding a list of objects. Each stop object may have the keys "id", "name" and "description".

For every stop, in the order given, build the string

<name> - <description>
  • If "name" is absent, use the empty string in its place.
  • If "description" is absent, use the empty string in its place. An empty description stays empty. The separator - (space, hyphen, space) is always present, so the line still ends with " - ".
  • If the "stops" key is absent, the result for this part is an empty list.

Part B: Reporting-tree outline

staff_rows is a list of rows. Each row has exactly four strings:

[employee_name, title, department, manager_name]
  • manager_name is the empty string for a top-level leader.
  • Every non-empty manager_name is the employee_name of some row in the list.
  • Employee names are unique, and the reporting relation has no cycles.
  • There may be zero, one, or several top-level leaders.

Produce a list of strings, one per employee, that lists the organisation as an outline:

  • A top-level leader is at depth 0, their direct reports at depth 1, and so on.
  • Each line is the employee name preceded by 2 * depth space characters.
  • Every employee appears directly after the lines of the previous sibling's whole subtree, so each manager is followed immediately by their own subtree (pre-order).
  • Sibling groups are sorted in ascending lexicographic (plain string comparison) order by employee_name. This applies to the top-level leaders as well as to each manager's direct reports.
  • The row order in the input is arbitrary and carries no meaning.

Return value

Return a dictionary with exactly two keys:

{"stops": <list of strings from Part A>, "org_chart": <list of strings from Part B>}

The input can be large, so a solution that rescans all rows for every manager is too slow. Aim for roughly O(n log n) time.

Constraints

  • payload_text is always valid JSON and its top level is an object.
  • The number of stops is at most 10^5.
  • 0 <= len(staff_rows) <= 2 * 10^5.
  • Reporting chains can be very deep (up to the number of employees), so avoid relying on deep recursion.
  • Names contain no commas or newlines.

Examples

Example 1

Input: stops_and_org_outline('{"stops":[{"id":"123","name":"South Station","description":"Commuter rail hub"},{"id":"456","name":"Park Street","description":"Red/Green lines"}]}', [['Alice', 'CEO', 'Executive', ''], ['Bob', 'CFO', 'Finance', 'Alice'], ['Carol', 'Analyst', 'Finance', 'Bob'], ['Dave', 'Intern', 'Finance', 'Carol'], ['Eve', 'COO', 'Operations', 'Alice'], ['Frank', 'CTO', 'Engineering', 'Alice']])

Output: {'stops': ['South Station - Commuter rail hub', 'Park Street - Red/Green lines'], 'org_chart': ['Alice', ' Bob', ' Carol', ' Dave', ' Eve', ' Frank']}

Example 2

Input: stops_and_org_outline('{"stops":[{"id":"1","name":"Central","description":""},{"id":"2","name":"Mystic"}]}', [['Zoe', 'CEO', 'Exec', ''], ['Amy', 'Engineer', 'Tech', 'Zoe'], ['Ben', 'Engineer', 'Tech', 'Zoe'], ['Ann', 'Intern', 'Tech', 'Amy']])

Output: {'stops': ['Central - ', 'Mystic - '], 'org_chart': ['Zoe', ' Amy', ' Ann', ' Ben']}

Example 3

Input: stops_and_org_outline('{"stops":[]}', [])

Output: {'stops': [], 'org_chart': []}

Hints

Hint 1

Treat the two parts separately: json.loads plus dict.get with an empty-string default handles the stops, and the rest is a tree problem.

Hint 2

Build a map from each manager name to the list of their direct reports in one pass over the rows, then sort every list. Employees whose manager is the empty string are the roots.

Hint 3

Use pre-order DFS with an explicit stack to avoid recursion limits. Push children in reverse sorted order so they pop in ascending order, and store the depth with each stack entry to build the indentation.