Find Users Who Traded on Three or More Consecutive Calendar Days

Quick 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.

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.

Robinhood logo
Robinhood
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

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

ColumnTypeMeaning
trade_idINTUnique id of the trade. Primary key.
user_idINTThe user who placed the trade. NOT NULL.
trade_dateDATECalendar date of the trade. NOT NULL.
amountDECIMAL(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_iduser_idtrade_dateamount
11012024-01-01500
21012024-01-02300
31012024-01-03700
41022024-01-01200
51022024-01-03400
61032024-01-05100
71032024-01-06250
81032024-01-07350
91032024-01-08150

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.