KatInterview

Price Card Payments with Tiered Rates and Running Merchant Totals

Arrays & HashingAuto-graded · 22 test cases
MediumAuto-gradedArrays & Hashinghash-mapparsingsimulation

Last updated

Parse comma-separated payment rows, build a per-row rate from several rules plus a per-merchant running total, and round fees exactly to whole cents.

Card Payment Fee Calculator

A payment company bills each merchant a fee on every card payment it processes. The fee depends on whether the card was issued in the merchant's country, on the card network, and on how much that merchant has already processed earlier in the same batch.

You are given a list of payment rows, in processing order. Each row is a string of six comma-separated fields:

transaction_id,merchant_id,amount_cents,card_brand,merchant_country,card_country

Compute the fee for every row and return the results in the same order as the input.

Pricing rules

For each row, build a percentage rate as follows:

  1. Starting rate
    • If merchant_country equals card_country, start at 2.0%.
    • Otherwise, start at 3.0%.
  2. Network surcharge
    • If card_brand is AMEX, add 1.0%.
    • VISA and MASTERCARD add nothing.
  3. Loyalty discount
    • Look at the merchant's running total: the sum of amount_cents over all earlier rows in this batch with the same merchant_id.
    • If that total is at least 1,000,000, subtract 0.5% from the rate.
    • The current row's own amount is not part of the total used for its own fee. The row that pushes a merchant over the threshold is therefore not discounted, but later rows for that merchant are.
  4. Percentage fee
    • Compute amount_cents * rate / 100 and round it to a whole number of cents, with exact halves rounded up (that is, floor(value + 0.5)).
  5. Flat fee
    • Add a flat 30 cents to every row, including rows whose rounded percentage fee is 0.

The total fee for the row is the rounded percentage fee plus 30.

Running totals are kept separately per merchant and start at 0 for each call. Every row's amount is added to its merchant's total after that row's fee has been computed.

Task

Implement the function so that it returns a list of (transaction_id, fee_cents) pairs, one per input row, in input order. transaction_id is returned as a string and fee_cents as an integer.

Exact half-cent values must round up correctly, so avoid relying on binary floating-point results for the percentage computation.

Constraints

  • 1 <= len(records) <= 100,000
  • 1 <= amount_cents <= 100,000,000
  • card_brand is one of VISA, MASTERCARD, AMEX.
  • Country codes are 2-letter uppercase strings.
  • Transaction IDs are unique.
  • Rows have exactly six fields and there is no header row.

Examples

Example 1

Input: compute_payment_fees(['txn_1,m_1,10000,VISA,US,US', 'txn_2,m_1,5000,AMEX,US,CA', 'txn_3,m_2,20000,MASTERCARD,GB,GB'])

Output: [['txn_1', 230], ['txn_2', 230], ['txn_3', 430]]

Hints

Hint 1

Keep a dictionary from merchant_id to the total of that merchant's earlier amounts. Read it before computing the fee and update it only afterwards.

Hint 2

Every possible rate is a multiple of 0.5%, so you can store the rate as an integer number of half-percent units and avoid floats completely.

Hint 3

With the rate h in half-percent units, the percentage fee is amount * h / 200. Half-up rounding of that value equals (amount * h + 100) // 200.