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 Purchases table with the following structure:

Column NameType
user_idint
time_stampdatetime
amountint

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
Discount Eligibility ProcessPurchaseRecordsDate FilterAmount FilterEligibilityCheckโœ“ In Date Rangeโœ“ Min AmountEligibleUsersExample Walkthrough:Input: startDate='2022-04-01', endDate='2022-04-30', minAmount=400User 1: 2022-04-20, $500User 2: 2022-03-15, $300User 3: 2022-04-25, $600โœ“ Eligible (in range + amount โ‰ฅ 400)โœ— Not eligible (outside date range)โœ“ Eligible (in range + amount โ‰ฅ 400)Final Result: [1, 3]
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!
Asked in
Amazon 45 Google 32 Microsoft 28 Meta 22
38.2K Views
Medium Frequency
~15 min Avg. Time
1.6K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen