AlgoViz

Design a Fraud Detection System

hard 50 min

Judge 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

Our systemPayment serviceScoringrules and modelFeature storeRedisKafkadecisionsBank challenge

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

FraudHonest
All288,000287.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

    critical

    Approving everything scores 99.9%.

    Wrong
    assert accuracy(model) > 0.99
    Right
    report(precision, recall, money_saved, honest_stopped)
  • Training on today's features

    critical

    The row already knows what happened later.

    Wrong
    train = payments.join(features_now)
    Right
    train = decisions.logged_features
  • Approving everything on failure

    critical

    An outage becomes an open door.

    Wrong
    except Timeout:    return "approve"
    Right
    except Timeout:    return fallback(amount, blocked, can_challenge)
  • Counting a retry twice

    major

    A slow network makes an honest payer look busy.

    Wrong
    count = incr(card)
    Right
    count = add_once(card, payment_id)
  • Blocking a whole group

    major

    Thousands of honest people share a card range, country or address.

    Wrong
    block(country)
    Right
    challenge(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.