Employees Earning More Than Their Managers — LeetCode 181 Python Solution

EasyDatabase
Problem
#181
Reading time
2 min

The problem

List the names of the employees who earn strictly more than their own manager. The Employee table has id (int, primary key), name (varchar), salary (int) and managerId (int), which holds the id of another employee and is null for someone with no manager. Return the names in a column called Employee.

Example

Input
Employee(id, name, salary, managerId) = (1, 'Dana', 70000, 3), (2, 'Ravi', 80000, 4), (3, 'Sam', 60000, null), (4, 'Maya', 90000, null)
Output
Employee = 'Dana'
Explanation
Dana earns 70000 while her manager Sam earns 60000; Ravi earns less than his manager Maya.

Python solution

Python
import pandas as pd


def find_employees(employee: pd.DataFrame) -> pd.DataFrame:
    merged = employee.merge(
        employee, left_on="managerId", right_on="id", suffixes=("", "_manager")
    )
    result = merged[merged["salary"] > merged["salary_manager"]][["name"]]
    result.columns = ["Employee"]
    return result

Complexity

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

Related problems

Frequently asked questions

How hard is LeetCode 181. Employees Earning More Than Their Managers?
LeetCode 181. Employees Earning More Than Their Managers is rated Easy on LeetCode.
What topics does LeetCode 181. Employees Earning More Than Their Managers cover?
LeetCode 181. Employees Earning More Than Their Managers is tagged Database on LeetCode.

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