KatInterview

Transaction Fraud Pipeline with Behavioral Rules

Data Structure DesignAuto-graded · 18 test cases
MediumAuto-gradedData Structure Designsimulationparsingsliding-window

Last updated

Build a stateful stream processor that validates payment records, detects velocity and spending anomalies, and produces fixed-width audit logs.

Problem

Build a fraud detection system that processes a stream of payment records, applies validation and risk checks, and outputs a formatted audit trail.

Each input string represents one transaction in CSV format with exactly five comma-separated fields:

transaction_id,user_id,amount_cents,currency,timestamp_iso

Rule Evaluation

Check each transaction against the following rules in the order listed. When multiple rules are violated, report only the first matching rule:

  1. INVALID_FORMAT: The record fails structural validation if any of these conditions hold:

    • The record does not contain exactly 5 comma-separated fields
    • amount_cents is not a positive integer greater than zero (must match regex [0-9]+ and convert to a value > 0)
    • timestamp_iso does not conform to strict ISO-8601 UTC format YYYY-MM-DDTHH:MM:SSZ or represents an impossible date (e.g., February 30)
  2. UNSUPPORTED_CURRENCY: The currency field is not exactly one of USD, EUR, or GBP (case-sensitive).

  3. HIGH_VELOCITY: The user already has 2 or more approved transactions within the past 60 seconds, inclusive. Specifically, if the current transaction occurs at time t, count how many previously approved transactions for the same user occurred at time prev_t where t - 60 <= prev_t <= t.

  4. AMOUNT_SPIKE: The transaction amount exceeds three times the user's historical average of all previously approved transactions. This check only applies when the user has at least 2 prior approved transactions.

If no rule is violated, mark the transaction APPROVED with reason OK, and update the user's approval history (timestamp and amount).

Transactions appear in strictly increasing order by timestamp.

Output Format

Return a list of fixed-width strings, one per input record. Each line contains three columns separated by exactly two spaces:

  • TX_ID: Left-aligned, width 10 (truncate if longer)
  • STATUS: Left-aligned, width 8 (APPROVED or DECLINED)
  • REASON: Left-aligned, width 20 (OK for approved, otherwise the rule name)

Constraints

  • 1 ≤ number of records ≤ 10,000
  • transaction_id and user_id: alphanumeric strings, up to 32 characters
  • amount_cents: integer up to 10⁹
  • Transactions are sorted in strictly ascending order by timestamp_iso

Examples

Example 1

Input: process_transaction_stream(['tx_01,user_a,1000,USD,2023-01-01T00:00:00Z', 'tx_02,user_a,1200,USD,2023-01-01T00:00:15Z', 'tx_03,user_a,1100,USD,2023-01-01T00:00:30Z', 'tx_04,user_a,5000,USD,2023-01-01T00:02:00Z'])

Output: ['tx_01 APPROVED OK ', 'tx_02 APPROVED OK ', 'tx_03 DECLINED HIGH_VELOCITY ', 'tx_04 DECLINED AMOUNT_SPIKE ']

Hints

Hint 1

Maintain per-user state: a sliding window of approved timestamps and a running sum/count for the average. Declined transactions never contribute to either.

Hint 2

Use a deque for the velocity window. Since timestamps arrive in order, you can pop expired entries from the left without rescanning.

Hint 3

To avoid floating-point precision issues in the spike check, compare integers: amount * count > 3 * sum.