Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Print Company Hierarchy

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Print Company Hierarchy. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Hulu needs a readable representation of its employee reporting structure. Given each employee and their direct manager, format the hierarchy from the top-level leader downward.

Implement format_hierarchy(employees). The input is a dictionary mapping an employee name to their manager's name. The single root employee maps to None. Return one string containing one employee per line, in preorder traversal. Indent each reporting level with two spaces. Preserve the order in which employees appear in the input dictionary when listing peers.

The function should not print directly, because returning the formatted string makes the result easier to test. A caller can print the returned value.

Constraints

  • 1 <= number of employees <= 10^5
  • Employee names are unique, non-empty strings
  • Exactly one employee has manager None
  • Every non-root employee has exactly one manager
  • The hierarchy contains no cycles

Function Signature

def format_hierarchy(employees):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output