← Affirm

Affirm CodingFree previewFree

Fraud Detection Over Loan Events

Last asked

MedphoneonsiteSWE
SWEhashmappaymentcode-readingmedium

Problem

Work with a stream of loan events (underwriting and fraud_flag): debug given code, match transactions to fraud events, and list all fraud. The formalized versions count distinct PII values and flag suspicious underwriting events live.

A fraud-detection coding round in two to three parts, working with predefined domain objects. A record looks like:

{
  "customer_details": {
    "address": "8941 Curry St",
    "credit_score": 505,
    "email": "cash.olson@yahoo.com",
    "phone": "309-144-1261",
    "ssn": "329340719"
  },
  "event_type": "underwriting",
  "loan_amount": 256
}

with a second event type fraud_flag (which omits loan_amount), e.g.:

{
  "customer_details": {
    "address": "2143 Scarborough Ave",
    "credit_score": 774,
    "email": "hinson.parrott@hotmail.com",
    "phone": "117-570-8961",
    "ssn": "634841077"
  },
  "event_type": "fraud_flag"
}

Part 1 — Debug

Fix a bug in given code so that all test cases pass, without changing the expected external behavior (function signature, input/output structure). One variant stores the customer's PII fields into a set; another filters by event_type and guards whether a key exists in the detail map.

Part 2 — Match transactions to fraud events

Given a list of transactions and a list of fraud_events (plus a matching rule defined by the interviewer), attach/move each matched transaction's information into the corresponding Fraud Event, i.e. decide whether an event is fraud by correlating underwriting events with fraud_flag events. After processing, each Fraud Event should contain its associated transactions.

Part 3 — List all fraud (some loops)

From the processed structure, return "all fraud" (typically all transactions marked as fraud, or all fraud events containing fraudulent transactions; exact return type depends on the interviewer).

Formalized versions

Recovered from a 2026-07 phone screen; these are the precise specs of the two coding parts.

A) Count Distinct PII Values in Underwriting Events — implement an event consumer DistinctPIIValuesCounter over a sequential stream of JSON events. Count the number of DISTINCT PII values appearing across all events with event_type "underwriting". The PII fields are exactly: address, phone, email, ssn. Rules:

  • Only process underwriting events (ignore all other types).
  • Missing fields, empty strings, and null values are not counted.
  • credit_score is NOT PII.
  • Identical string values count once even across different fields or events.

Print one integer.

B) Live Fraud Detector — implement a FraudDetector whose handle_event processes events in input order:

  • fraud_flag event: record all non-empty PII values in the event as suspicious; return "" (print an empty line).
  • underwriting event: if ANY PII value in the event is already in the suspicious set, the transaction is suspicious; return "1", otherwise return "0". Important: when an underwriting event is suspicious, all of its non-empty PII values are ALSO added to the suspicious set (suspicious PII propagates transitively to future events).

Input

A list of transactions and a list of fraud_events (predefined objects as above); the matching rule/criteria is defined by the interviewer. Formalized versions: stdin contains a JSON array of event objects.

Output

Part 1: fixed code passing all tests. Part 2: updated fraud_events containing attached transaction information. Part 3: a collection representing all fraud. Formalized A: one integer (distinct PII count). Formalized B: one line per event, 0/1 for underwriting, an empty line for fraud_flag.

Constraints

  • Formalized versions: 0 <= n <= 100,000 events; at most 4 PII fields per event; each string length <= 1000; expected O(1) average processing per event (and O(1) query for the counter).

Examples

Example 1

Input

(Count Distinct PII) [ {"event_type": "underwriting", "customer_details": {"address": "123 Main St", "phone": "182-920-4124", "email": "johndoe@gmail.com", "ssn": "4568698929"}, "loan_amount": 3000}, {"event_type": "fraud_flag", "customer_details": {"address": "123 Main St", "phone": "182-920-4124", "email": "johndoe@gmail.com", "ssn": "4568698929"}}, {"event_type": "underwriting", "customer_details": {"address": "123 Main St", "phone": "947-213-9402", "email": "janedoe@yahoo.com", "ssn": "4568698929"}, "loan_amount": 3000} ]

Output

6

Explanation

Distinct PII values across the two underwriting events only: 123 Main St, 182-920-4124, 947-213-9402, johndoe@gmail.com, janedoe@yahoo.com, 4568698929 — the fraud_flag event is ignored and duplicates count once.

Example 2

Input

(Live Fraud Detector) events: underwriting(john's PII), fraud_flag(john's PII), underwriting(123 Main St, 947-213-9402, janedoe@yahoo.com, ssn 4568698929), underwriting(654 5th Ave, 947-213-9402, jamesdoe@hotmail.com, ssn 938103583)

Output

0 <empty line> 1 1

Explanation

Event 1 matches nothing suspicious (0). The fraud_flag marks john's PII suspicious. Event 3 matches the suspicious address+ssn (1) and its own new PII (947-213-9402, janedoe@yahoo.com) becomes suspicious too. Event 4 matches 947-213-9402, which became suspicious transitively in step 3 (1).

Follow-ups

  • Real-world / production considerations for the fraud-detection module.