KatInterview

CSV Transaction Parser with Validation and Error Handling

StringsModel answer
MediumModel answerStringsdata-validationparsingerror-handling

Last updated

Design a pipeline that parses CSV transaction records, normalizes fields, validates data integrity, and segregates malformed entries.

Problem Statement

Design and implement a function that processes raw transaction records provided as CSV strings. The pipeline must parse each record, apply normalization rules, validate data integrity, and maintain an audit trail of rejected entries.

Your function signature is:

def process_transactions(raw_data: list[str]) -> dict

The function should return a dictionary with two keys:

  • "cleaned": a list of validated transaction dictionaries
  • "rejected": a list of raw strings that failed validation

Each input string represents one CSV row with four columns: transaction_id, timestamp, amount, currency.

Requirements

Parsing

  • Use proper CSV parsing to handle quoted fields and embedded commas
  • Each row must contain exactly four fields
  • Skip any header row (identified by column names matching the expected fields)

Normalization

  • Strip leading and trailing whitespace from all fields
  • Handle currency symbols embedded in amounts (e.g., $100.50 should extract 100.50 and infer USD)
  • Recognize common currency aliases: $, US$, €, £, etc., and map them to standard three-letter codes
  • Preserve at least two decimal places for all amounts (pad if necessary, never round)

Validation

  • Reject rows where transaction_id is missing or empty after trimming
  • Reject rows where amount is not a valid decimal number
  • Reject rows with non-finite amounts (NaN, Infinity)
  • Reject rows where currency cannot be resolved to a valid three-letter code
  • Reject duplicate transaction IDs (first occurrence wins)
  • Reject rows with conflicting currency indicators (e.g., $100 in the amount field but EUR in the currency field)

Precision

  • Use exact decimal arithmetic to avoid floating-point errors
  • Return amounts as strings formatted with fixed-point notation (no scientific notation)

Error Handling

  • Never allow a single malformed row to crash the entire pipeline
  • Preserve the original raw string for every rejected entry

Example

Input:

[
    "transaction_id,timestamp,amount,currency",
    "tx_001,2023-10-01,100.50,USD",
    "tx_002,2023-10-01,invalid,USD",
    ",2023-10-01,50.00,EUR",
    "tx_003,2023-10-01, 75.25 , EUR ",
    "tx_004,2023-10-02,$99.99,",
    "tx_001,2023-10-02,200.00,USD"
]

Expected output:

{
    "cleaned": [
        {"transaction_id": "tx_001", "timestamp": "2023-10-01", "amount": "100.50", "currency": "USD"},
        {"transaction_id": "tx_003", "timestamp": "2023-10-01", "amount": "75.25", "currency": "EUR"},
        {"transaction_id": "tx_004", "timestamp": "2023-10-02", "amount": "99.99", "currency": "USD"}
    ],
    "rejected": [
        "tx_002,2023-10-01,invalid,USD",
        ",2023-10-01,50.00,EUR",
        "tx_001,2023-10-02,200.00,USD"
    ]
}

Constraints

  • Use Python standard library only
  • Output must be deterministic (same input always produces same output, same order)
  • Negative amounts are allowed (they represent refunds or reversals)

Hints

Hint 1

Use Python's csv module to handle quoted fields and commas correctly

Hint 2

The Decimal class from the decimal module provides exact arithmetic for currency values

Hint 3

Structure your validation as independent checks that each raise a specific exception when violated