Friday Purchases I — LeetCode 2993 Python Solution

MediumLeetCode PremiumDatabase
Problem
#2993
Reading time
3 min

Table schema

SQL
Table: Purchases +---------------+------+ | Column Name | Type | +---------------+------+ | user_id | int | | purchase_date | date | | amount_spend | int | +---------------+------+ (user_id, purchase_date, amount_spend) is the primary key (combination of columns with unique values) for this table. purchase_date will range from November 1, 2023, to November 30, 2023, inclusive of both dates.

Example

SQL
+---------------+------+
| Column Name   | Type |
+---------------+------+
| user_id       | int  |
| purchase_date | date |
| amount_spend  | int  |
+---------------+------+
(user_id, purchase_date, amount_spend) is the primary key (combination of columns with unique values) for this table.
purchase_date will range from November 1, 2023, to November 30, 2023, inclusive of both dates.
Each row contains user id, purchase date, and amount spend.

Python solution

Python
import duckdb
import pandas as pd

def solution(purchases: pd.DataFrame) -> pd.DataFrame:
    con = duckdb.connect()
    con.register("Purchases", purchases)
    return con.execute("""SELECT
    CEIL(DAYOFMONTH(purchase_date) / 7) AS week_of_month,
    purchase_date,
    SUM(amount_spend) AS total_amount
FROM Purchases
WHERE DATE_FORMAT(purchase_date, '%Y%m') = '202311' AND DAYOFWEEK(purchase_date) = 6
GROUP BY 2
ORDER BY 1;""").df()

Complexity

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

Related problems

Frequently asked questions

How hard is LeetCode 2993. Friday Purchases I?
LeetCode 2993. Friday Purchases I is rated Medium on LeetCode.
What topics does LeetCode 2993. Friday Purchases I cover?
LeetCode 2993. Friday Purchases I is tagged Database on LeetCode.
Is LeetCode 2993. Friday Purchases I a premium problem?
Yes. LeetCode 2993. Friday Purchases I 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