A card is high-velocity if, for some transaction on that card, more than 5 transactions on the same card (including that transaction itself) occur within 10 minutes from that transaction's timestamp. Write a query that returns the distinct card_id values of all high-velocity cards, ordered by card_id ascending.
Input Table(s)
transactions
| Column | Type | Meaning |
|---|
| transaction_id | INT | Unique id of the transaction |
| card_id | INT | Card used for the transaction, NOT NULL |
| amount | DECIMAL(10,2) | Transaction amount, NOT NULL |
| transaction_time | DATETIME (TIMESTAMP in PostgreSQL) | When the transaction happened, NOT NULL |
Primary key: transaction_id.
Output Contract
-
One column:
card_id
.
-
One row per high-velocity card, with no repeated card ids.
-
Rows ordered by
card_id
ascending. Each
card_id
appears once, so the order is fully determined.
-
There are no computed numeric columns, so no rounding applies.
Example
transactions
| transaction_id | card_id | amount | transaction_time |
|---|
| 1 | 7 | 12.50 | 2024-03-01 09:00:00 |
| 2 | 7 | 8.00 | 2024-03-01 09:02:00 |
| 3 | 7 | 15.25 | 2024-03-01 09:04:00 |
| 4 | 7 | 3.99 | 2024-03-01 09:06:00 |
| 5 | 7 | 20.00 | 2024-03-01 09:08:00 |
| 6 | 7 | 9.10 | 2024-03-01 09:10:00 |
| 7 | 9 | 40.00 | 2024-03-01 10:00:00 |
| 8 | 9 | 5.00 | 2024-03-01 10:01:00 |
| 9 | 9 | 5.00 | 2024-03-01 10:02:00 |
| 10 | 9 | 5.00 | 2024-03-01 10:03:00 |
| 11 | 9 | 5.00 | 2024-03-01 10:04:00 |
| 12 | 9 | 60.00 | 2024-03-01 10:10:01 |
Expected result:
Card 7 qualifies: the window that starts at transaction 1 (09:00:00) runs to 09:10:00 inclusive and holds transactions 1 to 6, which is 6 transactions. Card 9 does not: the window starting at 10:00:00 ends at 10:10:00 and holds 5 transactions (transaction 12 is one second late), the window starting at 10:01:00 holds transactions 8 to 12, which is also 5, and every later window holds fewer.
Constraints and Clarifications
-
transactions
has between 1 and 10,000 rows.
-
card_id
is a positive integer.
-
The window for a transaction at time
t
runs from
t
to
t + 10 minutes
, inclusive on both ends. A transaction exactly 10 minutes after the anchor counts; one at 10 minutes and 1 second does not.
-
"More than 5" means at least 6 transactions in one window, with the anchor transaction counted.
-
No column can be NULL, and
transaction_id
is unique, so every row is a separate transaction.
-
Several transactions on the same card can share a timestamp. Each of them counts toward the window.
-
amount
plays no part in the rule.