Skip to main content

Friday Purchases I - Solution & Explanation

MediumPremiumFree on FleetCodeDatabase5 min readAsked at: TCS
Practice this problem

Problem Statement

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.
Each row contains user id, purchase date, and amount spend.

Write a solution to calculate the total spending by users on each Friday of every week in November 2023. Output only weeks that include at least one purchase on a Friday.

Return the result table ordered by week of month in ascending order.

The result format is in the following example.

 

Example 1:

Input: 
Purchases table:
+---------+---------------+--------------+
| user_id | purchase_date | amount_spend |
+---------+---------------+--------------+
| 11      | 2023-11-07    | 1126         |
| 15      | 2023-11-30    | 7473         |
| 17      | 2023-11-14    | 2414         |
| 12      | 2023-11-24    | 9692         |
| 8       | 2023-11-03    | 5117         |
| 1       | 2023-11-16    | 5241         |
| 10      | 2023-11-12    | 8266         |
| 13      | 2023-11-24    | 12000        |
+---------+---------------+--------------+
Output: 
+---------------+---------------+--------------+
| week_of_month | purchase_date | total_amount |
+---------------+---------------+--------------+
| 1             | 2023-11-03    | 5117         |
| 4             | 2023-11-24    | 21692        |
+---------------+---------------+--------------+ 
Explanation: 
- During the first week of November 2023, transactions amounting to $5,117 occurred on Friday, 2023-11-03.
- For the second week of November 2023, there were no transactions on Friday, 2023-11-10.
- Similarly, during the third week of November 2023, there were no transactions on Friday, 2023-11-17.
- In the fourth week of November 2023, two transactions took place on Friday, 2023-11-24, amounting to $12,000 and $9,692 respectively, summing up to a total of $21,692.
Output table is ordered by week_of_month in ascending order.

Approach Overview

Problem Overview: The database contains a Purchases table with purchase dates and amounts. You need to report the total purchase amount for every Friday in November 2023. The key tasks are filtering rows that fall on a Friday, restricting results to the November 2023 range, and aggregating the purchase totals for each date.

Approach 1: Filter by Friday Using Date Functions (O(n) time, O(1) space)

The direct solution uses MySQL date functions to detect Fridays and then aggregates purchase amounts. Functions like WEEKDAY() or DAYOFWEEK() allow you to determine the day of the week for each purchase_date. Filter rows where the weekday corresponds to Friday and restrict the range to November 2023 using MONTH(), YEAR(), or a date range filter such as BETWEEN '2023-11-01' AND '2023-11-30'. After filtering, group by the purchase date and compute the total with SUM(amount). This approach scans the table once, making it O(n) time where n is the number of purchase rows. Memory usage stays O(1) because aggregation happens within the query engine without additional structures.

This technique relies on SQL database operations like filtering and grouping. It is concise and performs well for large datasets because the database optimizer can use indexes on the date column if available.

Approach 2: Generate Fridays and Join Purchases (O(n + f) time, O(f) space)

If the requirement is to show every Friday in the month even when no purchases occurred, you first generate the list of Fridays in November 2023. This can be done using MySQL date arithmetic such as DATE_ADD() or a recursive sequence to produce all dates, then filtering to Fridays with WEEKDAY(date) = 4. Once the Friday list is generated, perform a LEFT JOIN with the Purchases table on the date field and aggregate using SUM(amount). Missing purchase rows produce NULL, which can be converted to 0 using COALESCE().

This pattern combines calendar generation with aggregation and is common in reporting queries. The complexity becomes O(n + f), where n is the number of purchase rows and f is the number of generated Friday dates (a very small constant for a single month). The approach demonstrates strong understanding of SQL SQL date handling and reporting logic.

Recommended for interviews: The date-function filtering approach is what interviewers typically expect. It shows you understand SQL aggregation, date filtering, and weekday extraction. If the problem requires displaying Fridays without purchases, generating a calendar table and using a LEFT JOIN demonstrates deeper knowledge of reporting queries and database design patterns.

Solution

The date functions we use include:

  • DATE_FORMAT(date, format): Formats a date as a string
  • DAYOFWEEK(date): Returns the day of the week for a date, where 1 represents Sunday, 2 represents Monday, and so on
  • DAYOFMONTH(date): Returns the day of the month for a date

First, we use the DATE_FORMAT function to format the date in the form of YYYYMM, then filter out the records of November 2023 that fall on a Friday. Next, we group the records by purchase_date and calculate the total consumption amount for each Friday.

Code

MySQL

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Filter by Friday with Date FunctionsO(n)O(1)Best for direct aggregation when only existing Friday purchases need to be reported
Generate Fridays and LEFT JOIN PurchasesO(n + f)O(f)Use when every Friday must appear in the result, even if no purchases exist

Video Solution

Leetcode MEDIUM 2993 - Friday Purchases I - Solved by Everyday Data Science | DAYNAME(), WEEK() • Everyday Data Science • 812 views views

Frequently Asked Questions

Is Friday Purchases I easy or hard?
Friday Purchases I is rated Medium difficulty with a high acceptance rate around 79%. The logic is straightforward if you are comfortable with SQL date functions, filtering conditions, and GROUP BY aggregation.
Friday Purchases I Python/Java solution
This problem is categorized under Database on LeetCode, so the expected solution is written in SQL (typically MySQL). Python or Java implementations are not required because the task is executed directly within the database query engine.
How to solve Friday Purchases I in O(n)?
Use MySQL date functions to filter rows where the purchase date falls on a Friday and within November 2023. Functions like WEEKDAY(purchase_date) = 4 or DAYOFWEEK(purchase_date) = 6 identify Fridays. Then group by purchase_date and calculate SUM(amount). The database processes the dataset in a single pass.
What is the best approach for Friday Purchases I?
The most efficient solution filters rows using SQL date functions such as WEEKDAY() or DAYOFWEEK() to identify Fridays and restricts results to November 2023. After filtering, group by purchase_date and compute SUM(amount). This runs in O(n) time because the query scans the table once.
Is Friday Purchases I asked at Google/Amazon/Meta?
Database aggregation and date-filtering problems like this frequently appear in SQL interviews at companies such as Amazon, Google, and Meta. The exact problem may vary, but the concepts of GROUP BY, date functions, and weekday filtering are commonly tested.
What data structure is used in Friday Purchases I?
The problem relies on relational database tables rather than traditional data structures. The key concepts are SQL aggregation, date functions, and grouping operations on the Purchases table.
What is the time complexity of Friday Purchases I?
The typical SQL solution runs in O(n) time where n is the number of rows in the Purchases table. The database scans rows, filters by date conditions, and performs aggregation with GROUP BY. Space complexity is O(1) since aggregation is handled internally by the database engine.

Ready to solve this problem?

Practice Friday Purchases I with our built-in code editor and test cases.

Practice on FleetCode