Find Customer Referee — LeetCode 584 Python Solution

EasyDatabase
Problem
#584
Reading time
2 min

The problem

List the names of the customers who were not referred by the customer with id 2, which includes everyone who was not referred by anybody. The Customer table has id (int, primary key), name (varchar) and referee_id (int), the id of the customer who referred them, or null when nobody did. Return the names in a column called name.

Example

Input
Customer(id, name, referee_id) = (1, 'Ana', null), (2, 'Ben', null), (3, 'Cleo', 2), (4, 'Dev', 1), (5, 'Eve', 2)
Output
name = 'Ana', 'Ben', 'Dev'
Explanation
Only Cleo and Eve carry referee_id 2; the rows with a null referee_id are kept because "not referred by 2" includes "not referred at all".

Python solution

Python
import pandas as pd

def find_customer_referee(customer: pd.DataFrame) -> pd.DataFrame:
    res = customer[(customer['referee_id'].isna()) | (customer['referee_id'] != 2)]
    return res[['name']]

Complexity

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

Related problems

Frequently asked questions

How hard is LeetCode 584. Find Customer Referee?
LeetCode 584. Find Customer Referee is rated Easy on LeetCode.
What topics does LeetCode 584. Find Customer Referee cover?
LeetCode 584. Find Customer Referee 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