Customer Order Frequency — LeetCode 1511 Python Solution

EasyLeetCode PremiumDatabase
Problem
#1511
Reading time
3 min

Table schema

SQL
Table: Customers +---------------+---------+ | Column Name | Type | +---------------+---------+ | customer_id | int | | name | varchar | | country | varchar | +---------------+---------+ customer_id is the column with unique values for this table. This table contains information about the customers in the company.

Example

SQL
+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| customer_id   | int     |
| name          | varchar |
| country       | varchar |
+---------------+---------+
customer_id is the column with unique values for this table.
This table contains information about the customers in the company.

Python solution

Python
import duckdb
import pandas as pd

def solution(customers: pd.DataFrame, product: pd.DataFrame, orders: pd.DataFrame) -> pd.DataFrame:
    con = duckdb.connect()
    con.register("Customers", customers)
    con.register("Product", product)
    con.register("Orders", orders)
    return con.execute("""SELECT customer_id, name
FROM
    Orders
    JOIN Product USING (product_id)
    JOIN Customers USING (customer_id)
WHERE YEAR(order_date) = 2020
GROUP BY 1
HAVING
    SUM(IF(MONTH(order_date) = 6, quantity * price, 0)) >= 100
    AND SUM(IF(MONTH(order_date) = 7, quantity * price, 0)) >= 100;""").df()

Complexity

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

Related problems

Frequently asked questions

How hard is LeetCode 1511. Customer Order Frequency?
LeetCode 1511. Customer Order Frequency is rated Easy on LeetCode.
What topics does LeetCode 1511. Customer Order Frequency cover?
LeetCode 1511. Customer Order Frequency is tagged Database on LeetCode.
Is LeetCode 1511. Customer Order Frequency a premium problem?
Yes. LeetCode 1511. Customer Order Frequency is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.

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