Register and Transfer Brazilian Receivables
Problem statement
Brazilian card transactions are registered as daily receivables. You are given two CSV strings.
transactionsCsvhas headercustomer_id,merchant_id,payout_date,card_type,amount.contractsCsvhas headercontract_id,merchant_id,payout_date,card_type,amount. It may contain only the header.
First aggregate all transactions by (merchant_id, card_type, payout_date), summing their amounts. Then apply every contract. A contract replaces the matching merchant receivable: remove that merchant row and create a row identified by the contract ID with the contract's card type, payout date, and amount.
Return CSV rows beginning with id,card_type,payout_date,amount. Sort the data rows by Unicode code points in ID, then card type, then payout date. When all three strings are equal, sort by amount numerically in ascending order. Write each output amount in base-10 notation without leading zeros.
Function
registerReceivables(transactionsCsv: String, contractsCsv: String) → String[]Examples
Example 1
transactionsCsv = "customer_id,merchant_id,payout_date,card_type,amount\ncust1,merchantA,2021-12-30,Visa,150\ncust2,merchantA,2021-12-30,Visa,200\ncust3,merchantB,2021-12-31,MasterCard,300\ncust4,merchantA,2021-12-30,Visa,50"contractsCsv = "contract_id,merchant_id,payout_date,card_type,amount"return = ["id,card_type,payout_date,amount","merchantA,Visa,2021-12-30,400","merchantB,MasterCard,2021-12-31,300"]The three matching merchant-A Visa transactions aggregate to 400. No contract replaces either receivable.
Example 2
transactionsCsv = "customer_id,merchant_id,payout_date,card_type,amount\ncust1,merchantA,2022-01-05,Visa,300\ncust2,merchantA,2022-01-05,Visa,200\ncust3,merchantB,2022-01-06,MasterCard,1000"contractsCsv = "contract_id,merchant_id,payout_date,card_type,amount\ncontract1,merchantA,2022-01-05,Visa,500"return = ["id,card_type,payout_date,amount","contract1,Visa,2022-01-05,500","merchantB,MasterCard,2022-01-06,1000"]The contract replaces merchant A's matching receivable. Merchant B's receivable remains.
Example 3
transactionsCsv = "customer_id,merchant_id,payout_date,card_type,amount\ncust1,merchantA,2022-01-07,Visa,500\ncust2,merchantA,2022-01-07,Visa,250\ncust3,merchantB,2022-01-08,MasterCard,1250\ncust4,merchantC,2022-01-09,Visa,1500"contractsCsv = "contract_id,merchant_id,payout_date,card_type,amount\ncontract1,merchantA,2022-01-07,Visa,750\ncontract2,merchantC,2022-01-09,Visa,1500"return = ["id,card_type,payout_date,amount","contract1,Visa,2022-01-07,750","contract2,Visa,2022-01-09,1500","merchantB,MasterCard,2022-01-08,1250"]Two merchant receivables are replaced by contracts. The remaining rows are returned in the required deterministic order.
Constraints
- Each CSV contains a valid fixed header and between
0and100000data rows. - Fields contain no commas, line breaks, or surrounding whitespace.
- IDs, card types, and payout dates are non-empty.
- Amounts are positive integers at most
10^9; every aggregate fits a signed64-bit integer. - Each contract maps to exactly one aggregated merchant receivable, and no two contracts map to the same receivable.