EasyNo. 0584

Find Customer Referee — LeetCode 584 Python Solution

Topics
Database
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

PythonSolution
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

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

Related problems

Related problems
No.ProblemDifficulty
0585Investments in 2016Medium
0586Customer Placing the Largest Number of OrdersEasy
0577Employee BonusEasy
0175Combine Two TablesEasy
0176Second Highest SalaryMedium
0177Nth Highest SalaryMedium

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.

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