EasyNo. 0577

Employee Bonus — LeetCode 577 Python Solution

Topics
Database
Reading time
2 min

The problem

Report the name and bonus of every employee whose bonus is less than 1000, and include the employees who have no bonus record at all, showing a null bonus for them. Employee has empId (int, unique), name (varchar), supervisor (int) and salary (int); Bonus has empId (int, primary key referencing an employee) and bonus (int). Return name and bonus.

Example

Input
Employee(empId, name, supervisor, salary) = (1, 'Ana', null, 5000), (2, 'Ben', 1, 3000), (3, 'Cleo', 1, 3500), (4, 'Dev', 1, 2500); Bonus(empId, bonus) = (2, 500), (3, 2000)
Output
('Ana', null), ('Ben', 500), ('Dev', null)
Explanation
Ben is under 1000, Ana and Dev have no bonus row at all, and Cleo's 2000 is excluded.

Python solution

PythonSolution
import pandas as pd

def employee_bonus(employee: pd.DataFrame, bonus: pd.DataFrame) -> pd.DataFrame:
    df = employee.merge(bonus, on='empId', how='left')
    res = df[(df['bonus'].isna()) | (df['bonus'] < 1000)]
    return res[['name', 'bonus']]

Complexity

Time
O(n log n)(typical)
Space · auxiliary
O(n)

Related problems

Frequently asked questions

How hard is LeetCode 577. Employee Bonus?

LeetCode 577. Employee Bonus is rated Easy on LeetCode.

What topics does LeetCode 577. Employee Bonus cover?

LeetCode 577. Employee Bonus 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.

Free to download. Check it runs on your machine, then pick a plan.