Using the trades table, find every user_id with at least one trade on each of 3 or more consecutive calendar days. A user may trade multiple times on one day. Return the distinct user_id values ordered ascending.
Input Table(s)
Table trades
| Column | Type | Meaning |
|---|
| trade_id | INT | Unique id of the trade. Primary key. |
| user_id | INT | The user who placed the trade. NOT NULL. |
| trade_date | DATE | Calendar date of the trade. NOT NULL. |
| amount | DECIMAL(10,2) | Trade amount. NOT NULL. |
Primary key: trade_id. A (user_id, trade_date) pair can appear in many rows.
Output Contract
-
Exactly one column,
user_id
.
-
One row per qualifying user, with no repeats.
-
Rows sorted by
user_id
ascending. Each value appears once, so the order is unique.
Example
trades
| trade_id | user_id | trade_date | amount |
|---|
| 1 | 101 | 2024-01-01 | 500 |
| 2 | 101 | 2024-01-02 | 300 |
| 3 | 101 | 2024-01-03 | 700 |
| 4 | 102 | 2024-01-01 | 200 |
| 5 | 102 | 2024-01-03 | 400 |
| 6 | 103 | 2024-01-05 | 100 |
| 7 | 103 | 2024-01-06 | 250 |
| 8 | 103 | 2024-01-07 | 350 |
| 9 | 103 | 2024-01-08 | 150 |
Expected result
User 101 traded on Jan 1, 2 and 3 (3 consecutive days). User 102 traded on Jan 1 and Jan 3, with a gap on Jan 2. User 103 traded on Jan 5, 6, 7 and 8 (4 consecutive days). So users 101 and 103 qualify.
Constraints and Clarifications
-
The table has at most 10,000 rows.
-
user_id
is a positive integer and
amount
is a positive decimal.
amount
does not affect the answer.
-
Consecutive means consecutive calendar days, weekends included. Trades on Friday, Saturday and Sunday form a run of three; trades on Friday and Monday do not touch.
-
Runs can cross month and year boundaries (Dec 31, Jan 1, Jan 2 is a run of three).
-
Several trades by the same user on the same day count as one day.
-
A user with a run longer than three, or with several runs, is returned once.