Find Users Who Traded on Three or More Consecutive Calendar Days
Company: Robinhood
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
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.
```hint Rows are not days
A user who trades five times on one day has five rows but only one trading day. Decide what one row should represent before you look for runs.
```
### 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_id |
|---|
| 101 |
| 103 |
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.
Overview: A SQL problem on a trades table: list every user who placed at least one trade on each of three or more consecutive calendar days, where a user can trade several times a day. It tests date arithmetic, collapsing repeated rows to one per day, and detecting runs of consecutive dates.
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.
Table `trades`:
- `trade_id` INTEGER: unique id of the trade. Primary key.
- `user_id` INTEGER NOT NULL: the user who placed the trade.
- `trade_date` DATE NOT NULL: calendar date of the trade.
- `amount` DECIMAL(10,2) NOT NULL: trade amount.
A (`user_id`, `trade_date`) pair can appear in many rows.
Output: exactly one column, `user_id`, with one row per qualifying user and no repeats, sorted by `user_id` ascending. Each value appears once, so the order is unique.
Rules:
- 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.
- `user_id` is a positive integer and `amount` is a positive decimal. `amount` does not affect the answer.
- The table has at most 10,000 rows.
Example: a user who traded on 2024-01-01, 2024-01-02 and 2024-01-03 qualifies. A user who traded on 2024-01-01 and 2024-01-03 does not, because 2024-01-02 is missing.
Tables
trades(trade_id INTEGER, user_id INTEGER, trade_date DATE, amount DECIMAL(10,2))
Hints
- A user who trades five times on one day has five rows but only one trading day. Decide what one row should represent before you look for runs.
- Compare full dates, since a run can pass through a weekend, a month end or a year end.