Find card payments repeated at the same merchant within 10 minutes
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
A payment is a repeated payment if another payment with the same `merchant_id`, `credit_card_id` and `amount` was made within the 10 minutes before it. Return `payment_id`, `merchant_id`, `credit_card_id`, `amount` and `payment_timestamp` of all repeated payments, ordered by `merchant_id`, then `payment_timestamp`, then `payment_id`.
```hint Watch the window edges
Decide exactly what "within 10 minutes before" means at the boundaries: a gap of exactly 10 minutes, a gap of 10 minutes and a few seconds, and two matching payments with the same timestamp. Some time functions round in ways that blur these cases.
```
### Input Table(s)
`payments`
| Column | Type | Meaning |
|---|---|---|
| payment_id | INT | Unique payment id |
| merchant_id | INT | Merchant that received the payment (NOT NULL) |
| credit_card_id | INT | Card used for the payment (NOT NULL) |
| amount | DECIMAL(10,2) | Payment amount (NOT NULL) |
| payment_timestamp | TIMESTAMP | When the payment was made, no time zone (NOT NULL) |
Primary key: `payment_id`.
### Output Contract
- Columns, in this order: `payment_id`, `merchant_id`, `credit_card_id`, `amount`, `payment_timestamp`, with values as stored.
- Each repeated payment appears exactly once, even if it matches several earlier payments.
- Ordered by `merchant_id` ascending, then `payment_timestamp` ascending, then `payment_id` ascending. `payment_id` is unique, so the order is fully determined.
### Example
Input `payments`:
| payment_id | merchant_id | credit_card_id | amount | payment_timestamp |
|---|---|---|---|---|
| 1 | 101 | 1 | 100.00 | 2022-09-25 12:00:00 |
| 2 | 101 | 1 | 100.00 | 2022-09-25 12:08:00 |
| 3 | 101 | 1 | 100.00 | 2022-09-25 12:28:00 |
| 4 | 101 | 1 | 100.00 | 2022-09-25 12:38:00 |
| 5 | 101 | 2 | 100.00 | 2022-09-25 12:01:00 |
| 6 | 102 | 1 | 100.00 | 2022-09-25 12:03:00 |
| 7 | 102 | 1 | 50.00 | 2022-09-25 12:05:00 |
| 8 | 102 | 1 | 100.00 | 2022-09-25 12:13:30 |
| 9 | 102 | 1 | 50.00 | 2022-09-25 12:14:00 |
Expected result:
| payment_id | merchant_id | credit_card_id | amount | payment_timestamp |
|---|---|---|---|---|
| 2 | 101 | 1 | 100.00 | 2022-09-25 12:08:00 |
| 4 | 101 | 1 | 100.00 | 2022-09-25 12:38:00 |
| 9 | 102 | 1 | 50.00 | 2022-09-25 12:14:00 |
Payment 2 comes 8 minutes after payment 1. Payment 3 is 20 minutes after its latest match, so it is not repeated, while payment 4 is exactly 10 minutes after payment 3 and counts. Payment 5 uses a different card and payment 7 a different amount, so neither has an earlier match. Payment 8 is 10 minutes 30 seconds after payment 6, which is outside the window. Payment 9 is 9 minutes after payment 7.
### Constraints and Clarifications
- Payment P is repeated when some payment Q has the same `merchant_id`, `credit_card_id` and `amount`, Q's timestamp is strictly earlier than P's, and the gap is at most 10 minutes (600 seconds). A gap of exactly 10 minutes counts. A gap of 10 minutes and 1 second does not.
- Two matching payments with the identical timestamp do not make each other repeated, since neither was made before the other.
- The first payment of a burst is never returned. A payment can be repeated because of an earlier payment that is itself repeated.
- `amount` is compared exactly, so 100.00 and 100.01 are different amounts.
- No column is NULL.
- `payments` has at most 10,000 rows.
Overview: Find every card payment that repeats an earlier payment with the same merchant, card and amount made within the previous 10 minutes. It tests correlated EXISTS subqueries or self-joins, exact time window boundaries, tie handling on equal timestamps and deduplication in SQL.