KatInterview

Classify Risky Transactions and Dispute History

SQL & DataAuto-graded · 16 test cases
MediumAuto-gradedSQL & Datadata-manipulationdatetimebusiness-rules

Last 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

fieldtypedescription
user_idintprimary key
birthdatestr (date)YYYY-MM-DD
countrystrregistered country code
created_atstr (timestamp)YYYY-MM-DD HH:MM:SS

payments — one row per transaction

fieldtypedescription
payment_idintprimary key
user_idintlinks to users
amount_usdfloattransaction amount in USD
currencystroriginal currency (informational)
payment_tsstr (timestamp)when the payment occurred
ip_countrystrcountry inferred from the request IP
device_idstrdevice identifier

chargebacks — one row per dispute

fieldtypedescription
cb_idintprimary key
payment_idintthe disputed payment
cb_tsstr (timestamp)when the chargeback was filed
reason_codestrdispute 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:

keytypedescription
user_idint
payments_last_7dintcount of payments in the 7-day window above
suspect_cnt_last_7dinthow many of those payments are "risky" (see below)
any_chargeback_last_60dstr"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:

  1. The chargeback is linked (via payment_id) to one of that user's payments.
  2. That payment's payment_ts falls in [2025-07-03 00:00:00, 2025-09-01 00:00:00).
  3. The chargeback's own cb_ts is ≤ 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 000
  • 1 ≤ len(payments) ≤ 100 000
  • 0 ≤ 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_usd is a non-negative float; the > 10 check is strict (exactly 10.00 is not risky).
  • payment_id values in chargebacks always refer to a payment that exists in payments.
  • 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 users does 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.