Find cards with more than 5 transactions in a 10-minute window
Company: Adyen
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
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.
```hint Counting per transaction
Treat each transaction as the start of its own 10-minute window and work out how many rows on the same card fall inside it. You need to compare rows of the table with other rows of the same table.
```
### 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_id |
|---|
| 7 |
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.
Overview: Write a SQL query that finds payment cards with more than five transactions inside a 10-minute window measured from any of the card's own transactions. It tests self-joins or range-framed window functions, inclusive time boundaries, and counting the anchor row correctly.
Table `transactions` holds one row per card transaction:
| Column | Type | Meaning |
|---|---|---|
| transaction_id | INTEGER | Unique id of the transaction (primary key) |
| card_id | INTEGER | Card used for the transaction, a positive integer, NOT NULL |
| amount | NUMERIC(10,2) | Transaction amount, NOT NULL |
| transaction_time | TIMESTAMP | When the transaction happened, NOT NULL |
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.
Rules:
- 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.
- Several transactions on the same card can share a timestamp. Each of them counts toward the window.
- `amount` plays no part in the rule.
- No column can be NULL, and `transaction_id` is unique, so every row is a separate transaction.
- `transactions` has between 1 and 10,000 rows.
Output: one column, `card_id`, with one row per high-velocity card and no repeated card ids, ordered by `card_id` ascending.
Tables
transactions(transaction_id INTEGER, card_id INTEGER, amount NUMERIC(10,2), transaction_time TIMESTAMP)
Hints
- Treat each transaction as the start of its own 10-minute window and work out how many rows on the same card fall inside it. You need to compare rows of the table with other rows of the same table.
- Test the window edges on your own data: rows that share the anchor's timestamp count, a row exactly 10 minutes later counts, and a card is listed once even when several of its windows qualify.