List every direct and indirect report under a manager

Quick Overview

Given a list of employee:manager pairs and a manager's name, return every direct and indirect report of that manager in sorted order. It tests building an adjacency map from parsed strings and walking a reporting hierarchy with breadth-first or depth-first search.

List every direct and indirect report under a manager

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a list of strings `relationships`. Each string has the form `"employee:manager"` and says that `employee` reports directly to `manager`. You are also given a name `manager`. Return a sorted list of every employee who reports to that manager, directly or indirectly. An indirect report is an employee who reports to someone who, through one or more steps up the chain, reports to the given manager. ### Function Signature ```python def get_reports(relationships: list[str], manager: str) -> list[str]: ``` ### Rules - Each string contains exactly one `:`. The part before it is the employee and the part after it is that employee's manager. - The given manager is not part of the result. - Each report appears once in the output, even if it can be reached through more than one relationship (for example, when the same line is listed twice). - Sort the result in ascending order by plain string comparison, which is the order Python's `sorted` gives. - If the given manager has no reports, return an empty list. ### Constraints - `1 <= len(relationships) <= 1000` - Every relationship has the form `"employee:manager"`. - Employee and manager names contain alphabetic characters only. - The given `manager` appears somewhere in `relationships`. - There are no circular reporting chains. ### Examples ```text Input: relationships = ["Bob:Alice", "Charlie:Bob", "Diana:Bob", "Eve:Alice", "Frank:Eve"], manager = "Alice" Output: ["Bob", "Charlie", "Diana", "Eve", "Frank"] ``` Alice manages Bob and Eve directly, Bob manages Charlie and Diana, and Eve manages Frank. All five people sit under Alice, listed in sorted order. ```text Input: relationships = ["Bob:Alice", "Charlie:Bob", "Diana:Bob", "Eve:Alice", "Frank:Eve"], manager = "Bob" Output: ["Charlie", "Diana"] ``` Bob manages Charlie and Diana directly, and neither of them manages anyone. ```text Input: relationships = ["Bob:Alice", "Charlie:Bob", "Diana:Bob", "Eve:Alice", "Frank:Eve"], manager = "Frank" Output: [] ``` Frank appears only as an employee, so nobody reports to him.

Overview: Given a list of employee:manager pairs and a manager's name, return every direct and indirect report of that manager in sorted order. It tests building an adjacency map from parsed strings and walking a reporting hierarchy with breadth-first or depth-first search.

Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a list of strings relationships. Each string has the form "employee:manager" and says that employee reports directly to manager. You are also given a name manager.

Return a sorted list of every employee who reports to that manager, directly or indirectly. An indirect report is an employee who reports to someone who, through one or more steps up the chain, reports to the given manager.

Function Signature

def get_reports(relationships: list[str], manager: str) -> list[str]:

Rules

  • Each string contains exactly one : . The part before it is the employee and the part after it is that employee's manager.
  • The given manager is not part of the result.
  • Each report appears once in the output, even if it can be reached through more than one relationship (for example, when the same line is listed twice).
  • Sort the result in ascending order by plain string comparison, which is the order Python's sorted gives.
  • If the given manager has no reports, return an empty list.

Constraints

  • 1 <= len(relationships) <= 1000
  • Every relationship has the form "employee:manager" .
  • Employee and manager names contain alphabetic characters only.
  • The given manager appears somewhere in relationships .
  • There are no circular reporting chains.

Examples

Input: relationships = ["Bob:Alice", "Charlie:Bob", "Diana:Bob", "Eve:Alice", "Frank:Eve"], manager = "Alice"
Output: ["Bob", "Charlie", "Diana", "Eve", "Frank"]

Alice manages Bob and Eve directly, Bob manages Charlie and Diana, and Eve manages Frank. All five people sit under Alice, listed in sorted order.

Input: relationships = ["Bob:Alice", "Charlie:Bob", "Diana:Bob", "Eve:Alice", "Frank:Eve"], manager = "Bob"
Output: ["Charlie", "Diana"]

Bob manages Charlie and Diana directly, and neither of them manages anyone.

Input: relationships = ["Bob:Alice", "Charlie:Bob", "Diana:Bob", "Eve:Alice", "Frank:Eve"], manager = "Frank"
Output: []

Frank appears only as an employee, so nobody reports to him.