Validate transactions with risk rules and reporting
Problem Description
You are tasked with building a fraud-screening validator that processes transaction data from a CSV file and generates a human-readable report. The validator must apply a series of risk rules and user behavior matching logic, then output a prioritized list of specific error codes for suspicious transactions.
Input Data
1. Transactions CSV
Each row in the CSV represents a single transaction and contains exactly six fields:
transaction_id(string)user_id(string)timestamp(string, assumed to be in ISO-8601 format, e.g.,2026-01-22T13:45:00Z)amount(string, which must be parseable as a decimal number)country(string, e.g.,US)payment_method(string, e.g.,VISA,PAYPAL) The first row of the CSV should be treated as a header and skipped.
2. Risk Configuration
This configuration defines global risk parameters:
min_amount: The minimum allowed transaction amount (inclusive).max_amount: The maximum allowed transaction amount (inclusive).blocked_payment_methods: A collection (set/list) of payment methods that are not permitted.
3. User Behavior Baseline
For each user_id, a baseline profile is provided with three behavioral features:
usual_countries: A collection (set/list) of countries where the user commonly transacts.usual_time_buckets: A collection (set/list) of predefined time-of-day buckets (e.g.,NIGHT,MORNING,AFTERNOON,EVENING) when the user commonly transacts.usual_amount_range: An inclusive range[low, high]representing the user's typical transaction amounts. You may assume that everyuser_idpresent in the transaction file will have a corresponding entry in the baseline data.
Processing Rules
Part 1: Verify Transaction Data Integrity
For every transaction, ensure that all six fields are non-empty after trimming any leading or trailing whitespace.
Part 2: High-Risk Rule Validation
In addition to Part 1, a transaction is flagged as suspicious if either of the following conditions is met:
- Amount Rule: The
amountis outside the[min_amount, max_amount]range, or theamountstring cannot be successfully parsed into a decimal number. - Payment Method Rule: The
payment_methodis included in theblocked_payment_methodslist.
Part 3: User Behavior Matching
Calculate how well a transaction aligns with the user's established behavioral baseline across the three specified features.
- Feature Extraction / Normalization: Convert the transaction's
timestampinto one of the definedusual_time_buckets. You must clearly define the rules for this time bucketing (e.g., specific hour ranges forMORNING,AFTERNOON, etc.). - Matching: Determine if each of the following features matches the user's baseline:
country: Matches if it is present inusual_countries.time_bucket: Matches if it is present inusual_time_buckets.amount: Matches if it falls withinusual_amount_range.
- Match Ratio: Compute
match_ratio = (number of matched features) / 3. Ifmatch_ratio < 0.5, the transaction should be flagged as suspicious due to behavior mismatch.
Part 4: Smart Fraud Error Reporting
Instead of a generic SUSPICIOUS status, output specific error codes. Adhere to the following rules:
- Output a maximum of two error codes per transaction.
- Error codes must be chosen based on the following priority order (highest priority first):
MISSING_FIELDAMOUNT_OUT_OF_RANGEBLOCKED_PAYMENT_METHODBEHAVIOR_MISMATCH
- If a transaction has no errors, output
OK.
Output Format
Produce a report with fixed-width and aligned columns for enhanced readability. The report must, at minimum, include:
transaction_iduser_idresult(This field will contain eitherOKor up to two comma-separated error codes).
Constraints
- The implementation must be efficient enough to process a large number of rows. Streaming processing is an acceptable approach.
- The time bucketing rules for
timestamptousual_time_bucketsmust be explicitly defined within the code. - Whitespace trimming is mandatory when checking for empty fields.
AI interview prep
Core dilemma
Balancing efficient, scalable processing of potentially large CSV files with the robust, extensible application of multiple, prioritized validation rules and detailed, structured error reporting.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Validate transactions with risk rules and reporting
Problem Description
You are tasked with building a fraud-screening validator that processes transaction data from a CSV file and generates a human-readable report. The validator must apply a series of risk rules and user behavior matching logic, then output a prioritized list of specific error codes for suspicious transactions.
Input Data
1. Transactions CSV
Each row in the CSV represents a single transaction and contains exactly six fields:
transaction_id(string)user_id(string)timestamp(string, assumed to be in ISO-8601 format, e.g.,2026-01-22T13:45:00Z)amount(string, which must be parseable as a decimal number)country(string, e.g.,US)payment_method(string, e.g.,VISA,PAYPAL) The first row of the CSV should be treated as a header and skipped.
2. Risk Configuration
This configuration defines global risk parameters:
min_amount: The minimum allowed transaction amount (inclusive).max_amount: The maximum allowed transaction amount (inclusive).blocked_payment_methods: A collection (set/list) of payment methods that are not permitted.
3. User Behavior Baseline
For each user_id, a baseline profile is provided with three behavioral features:
usual_countries: A collection (set/list) of countries where the user commonly transacts.usual_time_buckets: A collection (set/list) of predefined time-of-day buckets (e.g.,NIGHT,MORNING,AFTERNOON,EVENING) when the user commonly transacts.usual_amount_range: An inclusive range[low, high]representing the user's typical transaction amounts. You may assume that everyuser_idpresent in the transaction file will have a corresponding entry in the baseline data.
Processing Rules
Part 1: Verify Transaction Data Integrity
For every transaction, ensure that all six fields are non-empty after trimming any leading or trailing whitespace.
Part 2: High-Risk Rule Validation
In addition to Part 1, a transaction is flagged as suspicious if either of the following conditions is met:
- Amount Rule: The
amountis outside the[min_amount, max_amount]range, or theamountstring cannot be successfully parsed into a decimal number. - Payment Method Rule: The
payment_methodis included in theblocked_payment_methodslist.
Part 3: User Behavior Matching
Calculate how well a transaction aligns with the user's established behavioral baseline across the three specified features.
- Feature Extraction / Normalization: Convert the transaction's
timestampinto one of the definedusual_time_buckets. You must clearly define the rules for this time bucketing (e.g., specific hour ranges forMORNING,AFTERNOON, etc.). - Matching: Determine if each of the following features matches the user's baseline:
country: Matches if it is present inusual_countries.time_bucket: Matches if it is present inusual_time_buckets.amount: Matches if it falls withinusual_amount_range.
- Match Ratio: Compute
match_ratio = (number of matched features) / 3. Ifmatch_ratio < 0.5, the transaction should be flagged as suspicious due to behavior mismatch.
Part 4: Smart Fraud Error Reporting
Instead of a generic SUSPICIOUS status, output specific error codes. Adhere to the following rules:
- Output a maximum of two error codes per transaction.
- Error codes must be chosen based on the following priority order (highest priority first):
MISSING_FIELDAMOUNT_OUT_OF_RANGEBLOCKED_PAYMENT_METHODBEHAVIOR_MISMATCH
- If a transaction has no errors, output
OK.
Output Format
Produce a report with fixed-width and aligned columns for enhanced readability. The report must, at minimum, include:
transaction_iduser_idresult(This field will contain eitherOKor up to two comma-separated error codes).
Constraints
- The implementation must be efficient enough to process a large number of rows. Streaming processing is an acceptable approach.
- The time bucketing rules for
timestamptousual_time_bucketsmust be explicitly defined within the code. - Whitespace trimming is mandatory when checking for empty fields.