Calculate Special Bonus — LeetCode 1873 Python Solution

EasyDatabase
Problem
#1873
Reading time
3 min

Table schema

SQL
Table: Employees +-------------+---------+ | Column Name | Type | +-------------+---------+ | employee_id | int | | name | varchar | | salary | int | +-------------+---------+ employee_id is the primary key (column with unique values) for this table. Each row of this table indicates the employee ID, employee name, and salary.

Example

SQL
+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| employee_id | int     |
| name        | varchar |
| salary      | int     |
+-------------+---------+
employee_id is the primary key (column with unique values) for this table.
Each row of this table indicates the employee ID, employee name, and salary.

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
    employee_id,
    IF(
        employee_id % 2 = 0
        OR LEFT(name, 1) = 'M',
        0,
        salary
    ) AS bonus
FROM
    employees
ORDER BY 1;""").df()

Complexity

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

Related problems

Frequently asked questions

How hard is LeetCode 1873. Calculate Special Bonus?
LeetCode 1873. Calculate Special Bonus is rated Easy on LeetCode.
What topics does LeetCode 1873. Calculate Special Bonus cover?
LeetCode 1873. Calculate Special 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.

Get Stealth Interview