Price Card Payments with Tiered Rates and Running Merchant Totals
Arrays & HashingAuto-graded · 22 test casesLast 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:
- Starting rate
- If
merchant_countryequalscard_country, start at 2.0%. - Otherwise, start at 3.0%.
- If
- Network surcharge
- If
card_brandisAMEX, add 1.0%. VISAandMASTERCARDadd nothing.
- If
- Loyalty discount
- Look at the merchant's running total: the sum of
amount_centsover all earlier rows in this batch with the samemerchant_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.
- Look at the merchant's running total: the sum of
- Percentage fee
- Compute
amount_cents * rate / 100and round it to a whole number of cents, with exact halves rounded up (that is,floor(value + 0.5)).
- Compute
- 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,0001 <= amount_cents <= 100,000,000card_brandis one ofVISA,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.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Process the rows once, in order, while keeping a dictionary that maps each merchant_id to the total amount of that merchant's earlier rows.
For each row:
- Split it into its six fields and convert the amount to an integer.
- Look up the merchant's earlier total (default 0). This is read before the current amount is counted.
- Compute the rate in half-percent units: 4 for domestic (2.0%), 6 for cross-border (3.0%), plus 2 for AMEX (1.0%), minus 1 if the earlier total is at least 1,000,000 (0.5%). Every legal rate is a multiple of 0.5%, so an integer is enough.
- The percentage fee is
amount * h / 200. Half-up rounding of a non-negative fractionN / 200isfloor((N + 100) / 200), so use(amount * h + 100) // 200. - Add the flat 30 cents, append
(transaction_id, fee), and only then add the amount to the merchant's total.
Why it is correct
- Discount timing: the total is read before it is updated, so the row that crosses the threshold is not discounted, while all later rows are.
- Boundary: the check uses
>=, so a total of exactly 1,000,000 qualifies. - Per-merchant isolation: totals are keyed by merchant, so one merchant never affects another.
- Rounding: all arithmetic is on integers, so exact half-cent values (for example 3.0% of 50 cents is 1.5) round up reliably. Floating-point products like
50 * 0.03can land just below the half and round the wrong way. - Order: results are appended as rows are read, so output order matches input order.
Complexity
- Time: O(n) for n rows, since each row needs one split and constant dictionary work.
- Space: O(m + n), where m is the number of distinct merchants and n is the size of the output list.
Reference solution
from typing import List, Tuple
def compute_payment_fees(records: List[str]) -> List[Tuple[str, int]]:
totals = {}
output = []
THRESHOLD = 1_000_000
FLAT_FEE = 30
for row in records:
txn_id, merchant_id, amount_text, brand, merchant_country, card_country = row.split(',')
amount = int(amount_text)
earlier_total = totals.get(merchant_id, 0)
# Rate expressed in half-percent units (4 == 2.0%).
half_units = 4 if merchant_country == card_country else 6
if brand == 'AMEX':
half_units += 2
if earlier_total >= THRESHOLD:
half_units -= 1
pct_fee = (amount * half_units + 100) // 200
output.append((txn_id, pct_fee + FLAT_FEE))
totals[merchant_id] = earlier_total + amount
return output