Problem · String

Transaction Fee Calculator - Per-Merchant Volume Discount

MediumStripePHONE SCREEN
See Stripe hiring insights

This continues the transaction fee calculator. Now fees come from a passed-in country fee table and each merchant earns a volume discount based on how many completed transactions they have accumulated.

Inputs:

  • csvData: a CSV string with header then transactions; canonical columns include id, amount, buyer_country, transaction_type, payment_provider, status, merchant_id (amount in cents).
  • countryFees: a String[] where each entry is "country,provider,ratePercentAsDecimal,fixedCents" (e.g. "ie,card,0.019,20"). Use the row for the transaction's buyer_country; if that country is absent, fall back to the entries with country default; within a country, if the provider is missing, fall back to that country's (or default's) provider entry as available.

Base fee: base = amount * rate + fixed.

Volume discount (per merchant_id, by the merchant's cumulative count of completed transactions before the current one): counts 1-10 → 0%, 11-50 → 10%, 51-100 → 15%, 101+ → 20%. In other words, look up the discount using count_so_far + 1 (this transaction's ordinal among the merchant's completed transactions), then increment that merchant's count only for completed transactions. Non-completed transactions get a fee of 0 and do not change the count.

The final fee is floor(base * (1 - discount)). Process rows in input order. Return a String[]: header "id,transaction_type,payment_provider,fee" then one line per transaction in order.

Examples
01 · Example 1
csvData = "id,amount,buyer_country,transaction_type,payment_provider,status,merchant_id\nt1,1500,ie,payment,card,payment_completed,m1\nt2,1500,ie,payment,card,payment_completed,m1\nt3,1500,ie,payment,card,payment_completed,m1\nt4,1500,ie,payment,card,payment_completed,m1\nt5,1500,ie,payment,card,payment_completed,m1\nt6,1500,ie,payment,card,payment_completed,m1\nt7,1500,ie,payment,card,payment_completed,m1\nt8,1500,ie,payment,card,payment_completed,m1\nt9,1500,ie,payment,card,payment_completed,m1\nt10,1500,ie,payment,card,payment_completed,m1\nt11,1500,ie,payment,card,payment_completed,m1"
countryFees = ["ie,card,0.019,20", "ie,klarna,0.025,40", "ie,bank_transfer,0.006,0", "default,card,0.029,30", "default,klarna,0.035,50", "default,bank_transfer,0.008,0"]
return = ["id,transaction_type,payment_provider,fee", "t1,payment,card,48", "t2,payment,card,48", "t3,payment,card,48", "t4,payment,card,48", "t5,payment,card,48", "t6,payment,card,48", "t7,payment,card,48", "t8,payment,card,48", "t9,payment,card,48", "t10,payment,card,48", "t11,payment,card,43"]
Every transaction is m1, ie/card, completed, amount 1500, so base = 1500*0.019 + 20 = 48.5. Transactions t1..t10 are the merchant's 1st through 10th completed (tier 1-10, 0% discount), each fee floor(48.5) = 48. t11 is the 11th completed transaction (tier 11-50, 10% discount): 48.5 * 0.9 = 43.65, floored to 43.
02 · Example 2
csvData = "id,amount,buyer_country,transaction_type,payment_provider,status,merchant_id\nt1,1000,us,payment,card,payment_completed,m9\nt2,2000,us,payment,card,payment_failed,m9\nt3,1000,us,payment,card,payment_completed,m9"
countryFees = ["ie,card,0.019,20", "default,card,0.029,30", "default,klarna,0.035,50", "default,bank_transfer,0.008,0"]
return = ["id,transaction_type,payment_provider,fee", "t1,payment,card,59", "t2,payment,card,0", "t3,payment,card,59"]
us is absent from countryFees so it falls back to default card (0.029, 30). t1: 1st completed (0% tier) -> 1000*0.029 + 30 = 59. t2: failed -> fee 0 and the completed count does NOT advance. t3: still only the merchant's 2nd completed (0% tier) -> 59. The failed transaction did not consume a discount slot.
Constraints
  • countryFees entries are "country,provider,rate,fixed"; missing country falls back to default.
  • base = amount * rate + fixed; final fee = floor(base * (1 - discount)).
  • Discount by the merchant's completed-transaction ordinal: 1-10 -> 0%, 11-50 -> 10%, 51-100 -> 15%, 101+ -> 20%.
  • Only completed transactions earn a fee and advance the merchant's count; failed/pending get fee 0 and do not advance it.
  • Process rows in input order; return header then one line per transaction.
More Stripe problems
drafts saved locally
public String[] calculateFeesWithDiscount(String csvData, String[] countryFees) {
  // write your code here
}
csvData"id,amount,buyer_country,transaction_type,payment_provider,status,merchant_id\nt1,1500,ie,payment,card,payment_completed,m1\nt2,1500,ie,payment,card,payment_completed,m1\nt3,1500,ie,payment,card,payment_completed,m1\nt4,1500,ie,payment,card,payment_completed,m1\nt5,1500,ie,payment,card,payment_completed,m1\nt6,1500,ie,payment,card,payment_completed,m1\nt7,1500,ie,payment,card,payment_completed,m1\nt8,1500,ie,payment,card,payment_completed,m1\nt9,1500,ie,payment,card,payment_completed,m1\nt10,1500,ie,payment,card,payment_completed,m1\nt11,1500,ie,payment,card,payment_completed,m1"
countryFees["ie,card,0.019,20", "ie,klarna,0.025,40", "ie,bank_transfer,0.006,0", "default,card,0.029,30", "default,klarna,0.035,50", "default,bank_transfer,0.008,0"]
expected["id", "transaction_type", "payment_provider", "fee", "t1", "payment", "card", "48", "t2", "payment", "card", "48", "t3", "payment", "card", "48", "t4", "payment", "card", "48", "t5", "payment", "card", "48", "t6", "payment", "card", "48", "t7", "payment", "card", "48", "t8", "payment", "card", "48", "t9", "payment", "card", "48", "t10", "payment", "card", "48", "t11", "payment", "card", "43"]
sign in to submit