KatInterview

Card Settlement Batch with Fees, Rebates and Duplicate Guard

Data Structure DesignAuto-graded · 15 test cases
MediumAuto-gradedData Structure Designsimulationsortinghash-set

Last updated

Simulate a ledger: order card transactions by time, compute integer fee and rebate, enforce balance limits, and flag repeated transaction IDs.

Settling a Batch of Card Transactions

A company card platform receives a batch of settled card payments and must apply them to business account balances. Each payment may carry a flat processing fee and may earn a percentage rebate, depending on its merchant category. Write a function that settles the whole batch and reports what happened to each payment.

Inputs

  • transactions: a list of dictionaries, each with:
    • txn_id (str): the transaction identifier.
    • account_id (str): the business account that pays.
    • merchant_category (str): the category of the merchant, such as "SAAS", "TRAVEL" or "OFFICE".
    • amount_cents (int): the base amount in cents (positive).
    • timestamp (int): Unix time in seconds.
  • merchant_rules: a dictionary mapping each merchant category to a rule dictionary with:
    • rebate_bps (int): rebate rate in basis points (1 bp = 0.01%).
    • surcharge_cents (int): a flat fee in cents added to the payment.
    • exempt_threshold_cents (int): the fee is waived when amount_cents is strictly greater than this value.
  • account_balances: a dictionary mapping each account_id to its available balance in cents (int).

Rules

  1. Processing order. Handle transactions in ascending timestamp. If timestamps are equal, order by txn_id using ordinary string comparison (so "tx_10" comes before "tx_2").
  2. Cost of a transaction.
    • fee is 0 if amount_cents > exempt_threshold_cents; otherwise it is surcharge_cents. An amount exactly equal to the threshold still pays the fee.
    • rebate = floor(amount_cents * rebate_bps / 10000), computed with integers only.
    • net_cost = amount_cents + fee - rebate.
  3. Funds check. If the account's current balance is less than net_cost, the transaction is "DECLINED_INSUFFICIENT_FUNDS" and the balance does not change. A balance exactly equal to net_cost is sufficient. Otherwise the transaction is "PROCESSED" and net_cost is subtracted from the balance. Balances are updated as the batch is processed, so earlier transactions affect later ones.
  4. Duplicates. Once a txn_id has been encountered in processing order, it is used up, whether that first occurrence was processed or declined. Every later transaction with the same txn_id gets status "DUPLICATE" and has no effect on any balance. Note that the "first" occurrence is the first one in processing order, not in the input list.

Output

Return a two-element list [results, updated_balances]:

  • results: a list, in processing order, of dictionaries {"txn_id": str, "status": str, "net_cost_cents": int}. The status is one of "PROCESSED", "DECLINED_INSUFFICIENT_FUNDS" or "DUPLICATE". net_cost_cents is the computed net cost for "PROCESSED" entries and 0 for the others.
  • updated_balances: a new dictionary with the final balance of every account. The input account_balances must not be modified.

Constraints

  • Every merchant_category used by a transaction appears in merchant_rules, and every account_id appears in account_balances.
  • amount_cents is a positive integer; rebate_bps, surcharge_cents, exempt_threshold_cents and balances are non-negative integers.
  • The batch may be empty, in which case the result is [[], copy_of_balances].
  • Use integer arithmetic only; do not use floating-point numbers for the rebate.
  • The input lists and dictionaries should not be mutated.

Examples

Example 1

Input: settle_card_batch([{'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}], {'SAAS': {'rebate_bps': 150, 'surcharge_cents': 200, 'exempt_threshold_cents': 8000}}, {'acc_1': 6000})

Output: [[{'txn_id': 'tx_1', 'status': 'PROCESSED', 'net_cost_cents': 5125}, {'txn_id': 'tx_2', 'status': 'DECLINED_INSUFFICIENT_FUNDS', 'net_cost_cents': 0}, {'txn_id': 'tx_1', 'status': 'DUPLICATE', 'net_cost_cents': 0}], {'acc_1': 875}]

Hints

Hint 1

Sort a copy of the transactions by the pair (timestamp, txn_id) before doing anything else, because 'first occurrence' is defined by processing order.

Hint 2

Keep a set of txn_ids already seen. Add an id the first time you meet it regardless of whether the payment is processed or declined.

Hint 3

Compute the rebate as amount * bps // 10000 to stay in integers, and use a strict greater-than when testing the fee waiver. Work on a copy of the balances dictionary.