Implement and Defend an AI-Assisted Repository Change
RampSoftware Engineer · OnsiteLast 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
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.
merchant_rules: Dict mappingmerchant_categoryto 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): Ifamount_centsis strictly greater than this value,surcharge_centsis waived (set to 0).
account_balances: Dict mappingaccount_idtoavailable_balance_cents(int).
Business Rules
- Execution Order: Transactions must be processed in chronological order by
timestamp. In case of a tie, process alphabetically bytxn_id. - Net Calculation: For each transaction:
effective_surcharge = 0ifamount_cents > exempt_threshold_centselsesurcharge_cents.rebate = floor((amount_cents * rebate_bps) / 10000).net_cost = amount_cents + effective_surcharge - rebate.
- 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. - Idempotency: Duplicate transactions with the same
txn_idin 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 notPROCESSED,net_cost_centsmust be0.updated_balances: Copy of theaccount_balancesdict 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?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.