Classify Risky Transactions and Dispute History
SQL & DataAuto-graded · 16 test casesLast updated
Tests multi-window aggregation, business-rule encoding, and precise date/time boundary handling across three related data tables.
Classify Risky Transactions and Dispute History
You are building a fraud monitoring report for a payments platform. Given three tables of records as Python lists of dicts, produce a summary for every user who made at least one payment during a recent 7-day window.
Reference date: 2025-09-01 00:00:00 (treat this as "now").
Input tables
users — one row per account
| field | type | description |
|---|---|---|
user_id | int | primary key |
birthdate | str (date) | YYYY-MM-DD |
country | str | registered country code |
created_at | str (timestamp) | YYYY-MM-DD HH:MM:SS |
payments — one row per transaction
| field | type | description |
|---|---|---|
payment_id | int | primary key |
user_id | int | links to users |
amount_usd | float | transaction amount in USD |
currency | str | original currency (informational) |
payment_ts | str (timestamp) | when the payment occurred |
ip_country | str | country inferred from the request IP |
device_id | str | device identifier |
chargebacks — one row per dispute
| field | type | description |
|---|---|---|
cb_id | int | primary key |
payment_id | int | the disputed payment |
cb_ts | str (timestamp) | when the chargeback was filed |
reason_code | str | dispute category |
Task
Return one dict per user who has at least one payment with payment_ts in the half-open interval [2025-08-25 00:00:00, 2025-09-01 00:00:00). Each dict must contain exactly these keys:
| key | type | description |
|---|---|---|
user_id | int | |
payments_last_7d | int | count of payments in the 7-day window above |
suspect_cnt_last_7d | int | how many of those payments are "risky" (see below) |
any_chargeback_last_60d | str | "Y" or "N" (see below) |
Results must be sorted ascending by user_id.
Risky payment rule
A payment is risky if at least one of the following holds at the moment of the payment:
(a) Minor user — the account holder had not yet turned 18 by the date of the payment.
Compute the user's 18th birthday by advancing birthdate by exactly 18 years (if the birthday is Feb 29, map it to Feb 28 in non-leap years). A payment is on behalf of a minor when payment_ts.date() < 18th_birthday.
(b) IP country mismatch — users.country differs from payments.ip_country.
(c) New-account large purchase — the payment occurred within 24 hours of account creation (created_at <= payment_ts < created_at + 24 h) and amount_usd > 10.
Dispute history rule
any_chargeback_last_60d is "Y" for a user when all three conditions are met:
- The chargeback is linked (via
payment_id) to one of that user's payments. - That payment's
payment_tsfalls in[2025-07-03 00:00:00, 2025-09-01 00:00:00). - The chargeback's own
cb_tsis≤ 2025-09-01 00:00:00.
If no such chargeback exists, output "N". A user with no payments in the 7-day window must not appear in the output at all, even if they have qualifying disputes.
Constraints
1 ≤ len(users) ≤ 10 0001 ≤ len(payments) ≤ 100 0000 ≤ len(chargebacks) ≤ 50 000- All timestamp strings are valid and follow the format
YYYY-MM-DD HH:MM:SS. - All date strings are valid and follow the format
YYYY-MM-DD. amount_usdis a non-negative float; the> 10check is strict (exactly10.00is not risky).payment_idvalues inchargebacksalways refer to a payment that exists inpayments.- A single payment may have more than one chargeback row; count the user as having a dispute regardless of how many rows exist.
- A user with no matching row in
usersdoes not appear in the output.
Examples
Example 1
Input: classify_risky_transactions([{'user_id': 1, 'birthdate': '2012-05-10', 'country': 'US', 'created_at': '2023-11-01 09:00:00'}, {'user_id': 2, 'birthdate': '2000-03-03', 'country': 'US', 'created_at': '2022-02-14 10:00:00'}, {'user_id': 3, 'birthdate': '2010-12-31', 'country': 'CA', 'created_at': '2025-08-20 08:00:00'}], [{'payment_id': 10, 'user_id': 1, 'amount_usd': 4.99, 'currency': 'USD', 'payment_ts': '2025-08-28 12:00:00', 'ip_country': 'US', 'device_id': 'A'}, {'payment_id': 11, 'user_id': 1, 'amount_usd': 12.99, 'currency': 'USD', 'payment_ts': '2025-08-28 12:30:00', 'ip_country': 'US', 'device_id': 'A'}, {'payment_id': 12, 'user_id': 2, 'amount_usd': 19.99, 'currency': 'USD', 'payment_ts': '2025-08-31 20:00:00', 'ip_country': 'MX', 'device_id': 'B'}, {'payment_id': 13, 'user_id': 3, 'amount_usd': 0.99, 'currency': 'CAD', 'payment_ts': '2025-08-25 09:00:00', 'ip_country': 'CA', 'device_id': 'C'}], [{'cb_id': 100, 'payment_id': 12, 'cb_ts': '2025-09-15 10:00:00', 'reason_code': 'FRAUD_CARDHOLDER_DISPUTE'}])
Output: [{'user_id': 1, 'payments_last_7d': 2, 'suspect_cnt_last_7d': 2, 'any_chargeback_last_60d': 'N'}, {'user_id': 2, 'payments_last_7d': 1, 'suspect_cnt_last_7d': 1, 'any_chargeback_last_60d': 'N'}, {'user_id': 3, 'payments_last_7d': 1, 'suspect_cnt_last_7d': 1, 'any_chargeback_last_60d': 'N'}]
Hints
Hint 1
Start by indexing `users` by `user_id` and collecting each user's payments that fall inside the 7-day window before evaluating any rules — this avoids reprocessing payments you don't need.
Hint 2
For the risky-payment check, handle all three conditions inside a single pass over each payment: compute the 18th birthday with a try/except for leap-day birthdays, compare countries directly, and use a timedelta for the 24-hour new-account window.
Hint 3
For disputes, build a set of payment_ids whose chargebacks satisfy cb_ts ≤ end before you loop over users — then the per-user check is a simple membership test filtered by the 60-day payment window.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The core challenge is managing two independent time windows (7 days for payments, 60 days for disputes) without letting one corrupt the other. Joining disputes directly into payment rows would cause two problems: a payment with multiple chargeback records would be counted more than once, and the wider dispute window doesn't align with the narrower payment window. The solution pre-aggregates each concern separately and combines them only at the final step.
Step-by-step plan
1. Index the inputs.
Build a user_id → user dict and a payment_id → payment dict up front. Every subsequent lookup is O(1).
2. Find qualifying chargebacks.
Walk the chargebacks list once and record every payment_id whose cb_ts ≤ 2025-09-01. Store these in a plain set. The per-payment window check (payment_ts ∈ [2025-07-03, 2025-09-01)) happens later, once per user, so it never inflates payment counts.
3. Bucket payments into the 7-day window.
Walk the payments list once and group by user_id for payments with payment_ts ∈ [2025-08-25, 2025-09-01). Users who made no payment in this window simply never appear in the dict, so they're automatically excluded from output.
4. Evaluate each user.
For each user in ascending user_id order:
- Compute their 18th birthday once (with a
try/exceptfor Feb-29 leap birthdays that don't exist 18 years later, mapped to Feb 28). - For each recent payment, apply the three risky-payment conditions with a single
or. A payment only needs to satisfy one condition to be counted. - For disputes, filter the pre-built set to payments belonging to this user whose
payment_tsfalls in the 60-day window. Any match yields"Y".
Why the boundary rules are correct
- Both payment windows use half-open intervals (
>=start,<end), so a payment at exactly2025-09-01 00:00:00is excluded. - The chargeback cutoff uses
<=oncb_ts, matching the spec literally. - Age is checked by comparing
payment_ts.date()against the computed 18th birthday, not by subtracting years — year subtraction misclassifies anyone whose birthday hasn't occurred yet in the payment year. - The new-account window requires
timedelta(0) <= delta < timedelta(days=1), which naturally excludes payments that somehow predate account creation. - The
> 10check is strict, soamount_usd == 10.00is not risky.
Tracing the sample
| user | payments in 7d window | risky reasoning | dispute result |
|---|---|---|---|
| 1 | 10, 11 | Born 2012-05-10 → 18th birthday 2030-05-10; both payments in Aug 2025 → minor (rule a) | No chargeback with cb_ts ≤ 2025-09-01 |
| 2 | 12 | Adult; US ≠ MX → country mismatch (rule b) | Chargeback 100 has cb_ts = 2025-09-15 > 2025-09-01 → excluded |
| 3 | 13 | Born 2010-12-31 → 18th birthday 2028-12-31; payment in 2025 → minor (rule a). Rule c fails: amount is 0.99 ≤ 10 | No chargebacks |
User 2 is the key trap: the chargeback exists in the data but is post-dated relative to the as-of date, so any_chargeback_last_60d must still be "N".
Complexity
Let U = number of users, P = number of payments, C = number of chargebacks.
- Time: O(P + C + U log U) — one pass each over payments and chargebacks to build indexes, then O(U log U) for the sorted output. The per-user dispute scan is bounded by the number of chargebacks touching that user, summing to O(C) overall.
- Space: O(U + P + C) for the index dicts and the disputed-payment-id set.