Problem Β· String

Transaction Fee Calculator Part 3 β€” Merchant / Provider Waiver

Learn this problem
● MediumStripe logoStripeFULLTIMEPHONE SCREEN
See Stripe hiring insights

Problem statement

Compute transaction fees in input order and apply a volume waiver independently for every (merchant_id, payment_provider) pair.

Input

  • csvData has header id,amount,transaction_type,payment_provider,status,merchant_id; amount is a non-negative integer.
  • feeRules contains "provider,rate,fixed". For a completed transaction, its ordinary fee is floor(amount * rate + fixed).

Waiver

Track the cumulative amount of earlier payment_completed transactions for each merchant/provider pair. If that earlier cumulative amount is strictly greater than 10000, the current completed transaction's fee is 0. Otherwise charge the ordinary fee, then add the current amount to the pair's cumulative amount. Thus the transaction that first pushes the total above 10000 is still charged; only later transactions are waived.

Non-completed transactions have fee 0 and do not contribute to volume. Return header id,transaction_type,payment_provider,fee followed by one row per transaction.

Practice sequence

  1. Part 1: status gate and fee rules
  2. Part 3: merchant/provider waiver (current)

Function

calculateFeesWithWaiver(csvData: String, feeRules: String[]) β†’ String[]

Examples

Example 1

csvData = "id,amount,transaction_type,payment_provider,status,merchant_id\np1,6000,payment,card,payment_completed,m1\np2,5000,payment,card,payment_completed,m1\np3,100,payment,card,payment_completed,m1\np4,100,payment,card,payment_completed,m2"feeRules = ["card,0.02,30"]return = ["id,transaction_type,payment_provider,fee","p1,payment,card,150","p2,payment,card,130","p3,payment,card,0","p4,payment,card,32"]
m1/card exceeds 10000 only after p2, so p1 and p2 are charged and p3 is waived. m2 has an independent counter.

Example 2

csvData = "id,amount,transaction_type,payment_provider,status,merchant_id\na,11000,payment,card,payment_completed,m1\nb,500,payment,bank,payment_completed,m1\nc,100,payment,card,payment_failed,m1\nd,100,payment,card,payment_completed,m1"feeRules = ["card,0.01,10","bank,0.005,0"]return = ["id,transaction_type,payment_provider,fee","a,payment,card,120","b,payment,bank,2","c,payment,card,0","d,payment,card,0"]
The card total becomes 11000 after a, so later completed card transaction d is waived. Bank volume is separate, and failed transaction c neither pays a fee nor changes volume.

Constraints

  • The CSV uses the exact six-column header shown.
  • Every provider appearing in a transaction has exactly one fee rule.
  • Rates are non-negative decimal fractions and fixed fees are non-negative integers.
  • Only payment_completed rows are charged or added to volume.
  • The waiver key is the pair (merchant_id, payment_provider).
  • The threshold is strictly greater than 10000; the crossing transaction is charged.

More Stripe problems

drafts saved locally
public String[] calculateFeesWithWaiver(String csvData, String[] feeRules) {
  // write your code here
}
csvData"id,amount,transaction_type,payment_provider,status,merchant_id\np1,6000,payment,card,payment_completed,m1\np2,5000,payment,card,payment_completed,m1\np3,100,payment,card,payment_completed,m1\np4,100,payment,card,payment_completed,m2"
feeRules["card,0.02,30"]
expected["id", "transaction_type", "payment_provider", "fee", "p1", "payment", "card", "150", "p2", "payment", "card", "130", "p3", "payment", "card", "0", "p4", "payment", "card", "32"]
checking account