The Users That Are Eligible for Discount - Problem
Identify Discount-Eligible Users
You're working for an e-commerce platform that wants to reward loyal customers with discounts. Given a database of user purchases, your task is to find all users who made at least one purchase within a specific time window with a minimum spending threshold.
You have access to a
Eligibility Criteria:
A user qualifies for a discount if they made at least one purchase during the inclusive time period
Important: Dates should be treated as the start of the day (e.g., '2022-03-05' means '2022-03-05 00:00:00').
Goal: Return the user IDs of all eligible users, sorted in ascending order.
You're working for an e-commerce platform that wants to reward loyal customers with discounts. Given a database of user purchases, your task is to find all users who made at least one purchase within a specific time window with a minimum spending threshold.
You have access to a
Purchases table with the following structure:| Column Name | Type |
|---|---|
| user_id | int |
| time_stamp | datetime |
| amount | int |
Eligibility Criteria:
A user qualifies for a discount if they made at least one purchase during the inclusive time period
[startDate, endDate] where the purchase amount was at least minAmount.Important: Dates should be treated as the start of the day (e.g., '2022-03-05' means '2022-03-05 00:00:00').
Goal: Return the user IDs of all eligible users, sorted in ascending order.
Input & Output
example_1.sql โ Basic Eligibility Check
$
Input:
Purchases = [[1,'2022-04-20',500],[2,'2022-03-15',300],[1,'2022-05-01',200],[3,'2022-04-25',600]]
startDate = '2022-04-01', endDate = '2022-04-30', minAmount = 400
โบ
Output:
[1, 3]
๐ก Note:
User 1 has a purchase on 2022-04-20 with amount 500 (โฅ400). User 3 has a purchase on 2022-04-25 with amount 600 (โฅ400). User 2's purchase is outside the date range.
example_2.sql โ No Eligible Users
$
Input:
Purchases = [[1,'2022-03-10',100],[2,'2022-03-15',200]]
startDate = '2022-04-01', endDate = '2022-04-30', minAmount = 300
โบ
Output:
[]
๐ก Note:
No purchases fall within the specified date range, so no users are eligible for discount.
example_3.sql โ Multiple Purchases Same User
$
Input:
Purchases = [[1,'2022-04-10',100],[1,'2022-04-15',500],[1,'2022-04-20',200]]
startDate = '2022-04-01', endDate = '2022-04-30', minAmount = 400
โบ
Output:
[1]
๐ก Note:
User 1 appears multiple times but only needs one qualifying purchase (500 on 2022-04-15) to be eligible.
Constraints
- 1 โค purchases.length โค 104
- 1 โค user_id โค 1000
- 1 โค amount โค 106
- time_stamp format: 'YYYY-MM-DD'
- startDate โค endDate
Visualization
Tap to expand
Understanding the Visualization
1
Receive Purchase Records
Start with all purchase transactions from the database
2
Apply Date Filter
Check if purchase timestamp falls within [startDate, endDate]
3
Apply Amount Filter
Verify that purchase amount meets minimum threshold
4
Collect Eligible Users
Add qualifying users to our discount-eligible list
5
Return Sorted Results
Output unique user IDs in ascending order
Key Takeaway
๐ฏ Key Insight: We only need to find ONE qualifying purchase per user to mark them as eligible - this allows us to use a Set data structure to automatically handle duplicates while scanning through the data just once!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code