All People Report to the Given Manager — LeetCode 1270 Python Solution

MediumLeetCode PremiumDatabase
Problem
#1270
Reading time
3 min

Table schema

SQL
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.

Example

SQL
+---------------+---------+
| 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.

Python solution

Python
import duckdb
import pandas as pd

def solution(employees: pd.DataFrame) -> pd.DataFrame:
    con = duckdb.connect()
    con.register("Employees", employees)
    return con.execute("""SELECT e1.employee_id
FROM
    Employees AS e1
    JOIN Employees AS e2 ON e1.manager_id = e2.employee_id
    JOIN Employees AS e3 ON e2.manager_id = e3.employee_id
WHERE e1.employee_id != 1 AND e3.manager_id = 1;""").df()

Complexity

MeasureComplexity
TimeO(n log n) (typical)
SpaceO(n) auxiliary

Related problems

Frequently asked questions

How hard is LeetCode 1270. All People Report to the Given Manager?
LeetCode 1270. All People Report to the Given Manager is rated Medium on LeetCode.
What topics does LeetCode 1270. All People Report to the Given Manager cover?
LeetCode 1270. All People Report to the Given Manager is tagged Database on LeetCode.
Is LeetCode 1270. All People Report to the Given Manager a premium problem?
Yes. LeetCode 1270. All People Report to the Given Manager is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview