Brazilian Receivables Part 3 — Partial Contracts
Problem statement
Brazilian card transactions are registered as daily receivables. You are given two valid CSV strings.
transactionsCsvhas headercustomer_id,merchant_id,payout_date,card_type,amount.contractsCsvhas headercontract_id,merchant_id,payout_date,card_type,amount.
Aggregate transactions by (merchant_id, card_type, payout_date). Each contract buys only its stated amount from the matching merchant receivable. Create a contract-ID receivable for that amount and subtract it from the merchant receivable. Keep the merchant row when its remaining amount is positive; remove it when the remaining amount is zero.
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
allocatePartialContracts(transactionsCsv: String, contractsCsv: String) → String[]Examples
Example 1
transactionsCsv = "customer_id,merchant_id,payout_date,card_type,amount\ncust1,merchantA,2022-01-07,Visa,500"contractsCsv = "contract_id,merchant_id,payout_date,card_type,amount\ncontract1,merchantA,2022-01-07,Visa,200"return = ["id,card_type,payout_date,amount","contract1,Visa,2022-01-07,200","merchantA,Visa,2022-01-07,300"]The contract buys 200 of the 500 receivable, leaving 300 under the merchant ID.
Example 2
transactionsCsv = "customer_id,merchant_id,payout_date,card_type,amount\nc1,m1,2024-01-01,Visa,300\nc2,m1,2024-01-01,Visa,250\nc3,m2,2024-01-02,MasterCard,700"contractsCsv = "contract_id,merchant_id,payout_date,card_type,amount\nk1,m1,2024-01-01,Visa,550"return = ["id,card_type,payout_date,amount","k1,Visa,2024-01-01,550","m2,MasterCard,2024-01-02,700"]A full-amount contract removes the matching merchant receivable. The unrelated merchant remains.
Example 3
transactionsCsv = "customer_id,merchant_id,payout_date,card_type,amount\nc1,zeta,2024-02-01,Visa,100\nc2,alpha,2024-02-02,Amex,80"contractsCsv = "contract_id,merchant_id,payout_date,card_type,amount"return = ["id,card_type,payout_date,amount","alpha,Amex,2024-02-02,80","zeta,Visa,2024-02-01,100"]With no contracts, both aggregates remain and are sorted by output ID.
Constraints
- Each CSV contains its exact header followed by between
0and100000data rows. - Fields contain no commas, line breaks, or surrounding whitespace.
- Amounts are positive integers at most
10^9; every aggregate fits a signed64-bit integer. - Each contract maps to exactly one aggregated merchant receivable, its amount does not exceed that receivable, and no two contracts map to the same receivable.