Katkat

Implement and Defend an AI-Assisted Repository Change

RampSoftware Engineer · Onsite

Last updated

Tests your ability to implement a new feature in a simulated unfamiliar codebase using AI tools responsibly, identifying subtle generated bugs, ensuring test coverage, and defending every architectural and implementation decision.

Scenario

Imagine you are working in an unfamiliar codebase for an expense management platform. You are tasked with implementing a Dynamic Rebate & Surcharge Processor for corporate card transactions. You are encouraged to use an LLM/AI assistant to scaffold the code, but you must audit the output, verify edge cases, and defend every line of the final diff.

Requirements

Implement the class CardTransactionEngine with the method process_settlement_batch(transactions, merchant_rules, account_balances).

Input Formats

  1. transactions: List of dictionaries representing settled card swipes:
    • txn_id (str): Unique transaction identifier.
    • account_id (str): Unique business account identifier.
    • merchant_category (str): e.g., "SAAS", "TRAVEL", "OFFICE".
    • amount_cents (int): Base transaction amount in cents (positive integer).
    • timestamp (int): Unix timestamp in seconds.
  2. merchant_rules: Dict mapping merchant_category to a rule dict:
    • rebate_bps (int): Basis points (1 bp = 0.01%) refunded to the account.
    • surcharge_cents (int): Fixed platform processing fee added to the transaction.
    • exempt_threshold_cents (int): If amount_cents is strictly greater than this value, surcharge_cents is waived (set to 0).
  3. account_balances: Dict mapping account_id to available_balance_cents (int).

Business Rules

  1. Execution Order: Transactions must be processed in chronological order by timestamp. In case of a tie, process alphabetically by txn_id.
  2. Net Calculation: For each transaction:
    • effective_surcharge = 0 if amount_cents > exempt_threshold_cents else surcharge_cents.
    • rebate = floor((amount_cents * rebate_bps) / 10000).
    • net_cost = amount_cents + effective_surcharge - rebate.
  3. Insufficient Funds: An account cannot have a negative balance at any point. If available_balance_cents < net_cost, the transaction is marked as "DECLINED_INSUFFICIENT_FUNDS", and the account balance remains unchanged.
  4. Idempotency: Duplicate transactions with the same txn_id in the batch should be treated as duplicates: the first instance processes, and subsequent instances must be returned with status "DUPLICATE" without affecting balances.

Output Format

Return a tuple of (results, updated_balances):

  • results: List of dicts in processing order: {"txn_id": str, "status": "PROCESSED" | "DECLINED_INSUFFICIENT_FUNDS" | "DUPLICATE", "net_cost_cents": int}. If not PROCESSED, net_cost_cents must be 0.
  • updated_balances: Copy of the account_balances dict reflecting updated balances for processed transactions.

Example

transactions = [
    {"txn_id": "tx_2", "account_id": "acc_1", "merchant_category": "SAAS", "amount_cents": 10000, "timestamp": 105},
    {"txn_id": "tx_1", "account_id": "acc_1", "merchant_category": "SAAS", "amount_cents": 5000, "timestamp": 100},
    {"txn_id": "tx_1", "account_id": "acc_1", "merchant_category": "SAAS", "amount_cents": 5000, "timestamp": 110}
]
merchant_rules = {
    "SAAS": {"rebate_bps": 150, "surcharge_cents": 200, "exempt_threshold_cents": 8000}
}
account_balances = {"acc_1": 6000}

# Execution:
# 1. tx_1 (ts=100): amount=5000  surcharge=200. rebate = floor(5000 * 150 / 10000) = 75.
#    net_cost = 5000 + 200 - 75 = 5125. Balance 6000 >= 5125 -> PROCESSED. New balance = 875.
# 2. tx_2 (ts=105): amount=10000 > 8000 -> surcharge=0. rebate = floor(10000 * 150 / 10000) = 150.
#    net_cost = 9850. Balance 875  DECLINED_INSUFFICIENT_FUNDS.
# 3. tx_1 (ts=110): duplicate txn_id -> DUPLICATE.

Hints

Hint 1

Common LLM hallucinations include mutating the input dictionary in place and using floating point division rather than integer arithmetic (`//`).

Hint 2

Ensure tie-breaking logic handles both identical timestamps and unknown merchant categories gracefully if a default rule is not provided.

Hint 3

Be prepared to explain why you chose a specific order of operations (e.g., deduplication tracking vs. chronological sorting) and how your solution maintains idempotency across multiple function calls.

Follow-up questions

  • If an AI assistant generated a solution using floating-point math for the rebate calculation, what precision bugs could occur, and how do you write a test to catch them?
  • How would you refactor this batch function into an event-driven stream processor handling millions of events per second with high concurrency?
  • If you notice an AI-suggested helper function introduces a subtle O(N^2) lookup or deep-copy inside the processing loop, how would you optimize it without sacrificing readability?