#1270
Medium Database All people report to the given manager
Database
83.9% acceptance
Mar 31, 2026
444
32
No description available.
Solution
Pandas
Time O(n)
Space O(1)
# Table: Employees
#
# +---------------+---------+
# | Column Name | Type |
# +---------------+---------+
# | employee_id | int |
# | employee_name | varchar |
# | manager_id | int |
# +---------------+---------+
# employee_id is the column of unique values for this table.
# Each row of this table indicates that the employee with ID employee_id and name employee_name reports his work to his/her direct manager with manager_id
# The head of the company is the employee with employee_id = 1.
#
#
#
# Write a solution to find employee_id of all employees that directly or indirectly report their work to the head of the company.
#
# The indirect relation between managers will not exceed three managers as the company is small.
#
# Return the result table in any order.
#
# The result format is in the following example.
#
# Example 1:
# Input:
# Employees table:
# +-------------+---------------+------------+
# | employee_id | employee_name | manager_id |
# +-------------+---------------+------------+
# | 1 | Boss | 1 |
# | 3 | Alice | 3 |
# | 2 | Bob | 1 |
# | 4 | Daniel | 2 |
# | 7 | Luis | 4 |
# | 8 | Jhon | 3 |
# | 9 | Angela | 8 |
# | 77 | Robert | 1 |
# +-------------+---------------+------------+
# Output:
# +-------------+
# | employee_id |
# +-------------+
# | 2 |
# | 77 |
# | 4 |
# | 7 |
# +-------------+
# Explanation:
# The head of the company is the employee with employee_id 1.
# The employees with employee_id 2 and 77 report their work directly to the head of the company.
# The employee with employee_id 4 reports their work indirectly to the head of the company 4 --> 2 --> 1.
# The employee with employee_id 7 reports their work indirectly to the head of the company 7 --> 4 --> 2 --> 1.
# The employees with employee_id 3, 8, and 9 do not report their work to the head of the company directly or indirectly.
import pandas as pd
def find_reporting_people(employees: pd.DataFrame) -> pd.DataFrame:
manager_map = employees.set_index('employee_id')['manager_id'].to_dict()
result = []
for eid in employees['employee_id']:
if eid == 1:
continue
curr = eid
for _ in range(3):
curr = manager_map.get(curr)
if curr is None:
break
if curr == 1:
result.append(eid)
break
return pd.DataFrame({'employee_id': result})