Design a Fraud Detection System
hard 50 minJudge 10,000 payments a second, each within 100 ms, when only one in a thousand is fraud and the truth arrives weeks later.
See it run
One payment judged, one model trained, one case reviewed, one rule published.
Operations
Score a payment
From the request to approve, challenge or decline.
Step 1 of 8
Request
amount
50
deadline
100 ms
A token, an amount of 50, a merchant, a device and a deadline of 100 ms.
Step 1: Request
def score(pay, deadline): if pay.id in decided: return decided[pay.id] rows = features.add_and_read(pay, timeout_ms=15) if rules.hard(pay, rows): return save("decline") p = model(rows) # missing is allowed answer = cheapest(p, pay.amount) answer = rules.raise_caution(answer, pay, rows) return save(answer, p, rows) # log, then reply- Delay
- 45 ms planned, 100 ms limit
- Keeps
- a 2 KB record
The same payment id gives the same stored answer and counts once.
In the real world
The same design a fraud detection system doing real jobs. Pick one and step through it.
What a good model does to rare events.
Step 1 of 4
A day
| Fraud | Honest | |
|---|---|---|
| All | 288,000 | 287.7 million |
288 million payments, 288,000 of them fraud.
Step 1: A day
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Judging by accuracy
criticalApproving everything scores 99.9%.
Wrongassert accuracy(model) > 0.99Rightreport(precision, recall, money_saved, honest_stopped)Training on today's features
criticalThe row already knows what happened later.
Wrongtrain = payments.join(features_now)Righttrain = decisions.logged_featuresApproving everything on failure
criticalAn outage becomes an open door.
Wrongexcept Timeout: return "approve"Rightexcept Timeout: return fallback(amount, blocked, can_challenge)Counting a retry twice
majorA slow network makes an honest payer look busy.
Wrongcount = incr(card)Rightcount = add_once(card, payment_id)Blocking a whole group
majorThousands of honest people share a card range, country or address.
Wrongblock(country)Rightchallenge(merchant, device_type, expires="2h")
Interview tips
Multiply first
- 1 in 1,000 and 1%: 8.7% precision.
- 0.5% review is 14,400 people.
- So: challenge.
Spend the budget aloud
- 10, 15, 5, 3, 12 ms.
- Five rows in parallel.
- Trees on a CPU.
Show both loops
- Milliseconds: decide.
- Months: learn.
- Logged features join them.
Plan the failure
- Partial, rules only, fallback.
- The caller owns the deadline.
- Score again afterwards.