Skip to main content

The Users That Are Eligible for Discount - Solution & Explanation

EasyPremiumFree on FleetCodeDatabase4 min readAsked at: Analytics Quotient
Practice this problem

Problem Statement

Table: Purchases

+-------------+----------+
| Column Name | Type     |
+-------------+----------+
| user_id     | int      |
| time_stamp  | datetime |
| amount      | int      |
+-------------+----------+
(user_id, time_stamp) is the primary key (combination of columns with unique values) for this table.
Each row contains information about the purchase time and the amount paid for the user with ID user_id.

 

A user is eligible for a discount if they had a purchase in the inclusive interval of time [startDate, endDate] with at least minAmount amount. To convert the dates to times, both dates should be considered as the start of the day (i.e., endDate = 2022-03-05 should be considered as the time 2022-03-05 00:00:00).

Write a solution to report the IDs of the users that are eligible for a discount.

Return the result table ordered by user_id.

The result format is in the following example.

 

Example 1:

Input:
Purchases table:
+---------+---------------------+--------+
| user_id | time_stamp          | amount |
+---------+---------------------+--------+
| 1       | 2022-04-20 09:03:00 | 4416   |
| 2       | 2022-03-19 19:24:02 | 678    |
| 3       | 2022-03-18 12:03:09 | 4523   |
| 3       | 2022-03-30 09:43:42 | 626    |
+---------+---------------------+--------+
startDate = 2022-03-08, endDate = 2022-03-20, minAmount = 1000
Output:
+---------+
| user_id |
+---------+
| 3       |
+---------+
Explanation:
Out of the three users, only User 3 is eligible for a discount.
 - User 1 had one purchase with at least minAmount amount, but not within the time interval.
 - User 2 had one purchase within the time interval, but with less than minAmount amount.
 - User 3 is the only user who had a purchase that satisfies both conditions.

 

Important Note: This problem is basically the same as The Number of Users That Are Eligible for Discount.

Approach Overview

Problem Overview: You need to return the users who qualify for a discount based on their email format. The task is essentially a filtering problem: scan the Users table and keep only rows where the email satisfies the required domain pattern.

Approach 1: SQL Filtering with LIKE / REGEXP (O(n) time, O(1) space)

The solution performs a straightforward scan of the Users table and filters rows using a pattern match on the mail column. In MySQL, this is typically done using LIKE or REGEXP depending on how strict the validation needs to be. The key idea is to match emails that end with the required domain (for example '%@leetcode.com'). Since SQL evaluates the predicate for each row exactly once, the runtime is O(n) where n is the number of records. The query uses constant extra space because it only returns filtered rows without additional data structures.

Pattern matching is a common technique in database problems. The database engine scans each row, evaluates the predicate, and outputs matching records. When the requirement is strictly suffix-based, LIKE '%@domain.com' is usually enough. When stricter validation rules exist, MySQL REGEXP gives more control over allowed characters before the domain.

This approach works well because the task does not require joins, grouping, or aggregation. You simply iterate over the dataset once and apply a boolean condition to each row. That makes it both easy to write and efficient for large datasets.

From an interview perspective, this problem tests familiarity with SQL filtering and pattern matching rather than complex query construction. Recognizing that the entire problem reduces to a WHERE clause is the main insight. Topics such as SQL filtering and string pattern matching appear frequently in database interview questions.

Recommended for interviews: The direct filtering query using LIKE or REGEXP. It shows you understand how SQL handles pattern matching and how to efficiently filter rows without unnecessary joins or subqueries.

Solution

Code

MySQL

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
LIKE pattern filteringO(n)O(1)When checking if emails end with a specific domain suffix
REGEXP pattern validationO(n)O(1)When stricter validation of allowed characters is required before the domain

Video Solution

LeetCode 2230 "Users That Are Eligible for Discount" Interview SQL Question Detailed ExplanationEveryday Data Science1,450 views views

Frequently Asked Questions

Is The Users That Are Eligible for Discount easy or hard?
The problem is classified as Easy. It mainly tests basic SQL skills such as filtering rows with a WHERE clause and performing string pattern matching. No joins, aggregations, or advanced query constructs are required.
The Users That Are Eligible for Discount Python/Java solution
This problem is a database query rather than an algorithmic coding task, so the solution is written in SQL (MySQL on most platforms). In application code using Python or Java, you would typically execute the SQL query against the database and process the returned rows.
How to solve The Users That Are Eligible for Discount in O(n)?
Use a SQL query that filters rows based on the email domain condition. Apply a WHERE clause with a pattern such as LIKE '%@leetcode.com' or a REGEXP expression. The database scans the table once, checks the pattern for each row, and outputs the qualifying users.
What is the best approach for The Users That Are Eligible for Discount?
The best approach is a simple SQL filtering query using a WHERE clause with pattern matching. Most solutions use LIKE '%@leetcode.com' or REGEXP to match the required email format. The database scans the Users table once and returns matching rows, giving O(n) time complexity and O(1) extra space.
Is The Users That Are Eligible for Discount asked at Google/Amazon/Meta?
Database filtering and SQL pattern-matching problems appear frequently in interviews at companies like Amazon, Google, and Meta. While the exact problem may vary, the core skill tested—writing efficient WHERE conditions and understanding string pattern matching in SQL—is commonly evaluated.
What data structure is used in The Users That Are Eligible for Discount?
The problem operates directly on a relational database table. Instead of traditional data structures like arrays or hash maps, the solution relies on SQL table scanning and string pattern matching functions such as LIKE or REGEXP.
What is the time complexity of The Users That Are Eligible for Discount?
The time complexity is O(n), where n is the number of rows in the Users table. The database evaluates the filtering condition for each row exactly once during the table scan. Space complexity remains O(1) because the query does not allocate additional data structures.

Ready to solve this problem?

Practice The Users That Are Eligible for Discount with our built-in code editor and test cases.

Practice on FleetCode