Merchant Payout Scheduler, Part 2: Per-Merchant Schedules
Problem statement
Interview-style brief
This is Part 2 of the merchant payout scheduler. Instead of one shared schedule, every merchant now has its own first payout day and payout interval, and each transaction belongs to one merchant. Compute every payout, keeping different merchants apart.
Merchant Payout Scheduler Series
These exercises build on one another. Each page includes the rules needed to solve that part.
- Merchant Payout Scheduler, Part 1
- Merchant Payout Scheduler, Part 2: Per-Merchant Schedules (this part)
- Merchant Payout Scheduler, Part 3: Hold Periods
FastPrep practice contract
Each element of merchants has the format name,firstPayout,payoutInterval. Each element of transactions has the format id,amount,day,merchant, where amount is an integer number of cents and merchant names one of the merchants. Transactions may arrive in any order.
Payout schedule
A merchant pays out on days firstPayout, firstPayout + payoutInterval, firstPayout + 2 * payoutInterval, and so on.
A transaction on day d is paid on its merchant's first payout day that is strictly greater than d. As in Part 1, a transaction exactly on a payout day waits for the next one. A transaction before firstPayout is paid on firstPayout.
Payout lines
Group the transactions by merchant and payout day. Different merchants are never combined, even on the same day. Return one line for each group:
Day 14: acme $9.00 [t2, t5]Sum the amounts in integer cents and write the total in dollars with exactly two digits after the decimal point. List the IDs in their input order, separated by a comma and one space. Order the lines by payout day, and lines on the same day by merchant name in character-code order. Days and merchants without transactions produce no line, and no transactions means an empty array.
Function
getMerchantPayouts(merchants: String[], transactions: String[]) → String[]Examples
Example 1
merchants = ["cove,14,100","acme,7,7","bolt,5,10"]transactions = ["t1,1250,2,acme","t2,800,7,acme","t3,4000,4,bolt","t4,999,5,bolt","t5,100,13,acme","t6,50,13,bolt","t7,1,10,cove"]return = ["Day 5: bolt $40.00 [t3]","Day 7: acme $12.50 [t1]","Day 14: acme $9.00 [t2, t5]","Day 14: cove $0.01 [t7]","Day 15: bolt $10.49 [t4, t6]"]acme pays on days 7, 14, 21, .... t1 on day 2 is paid on day 7. t2 happens on day 7, which is a payout day, so it waits for day 14, together with t5 from day 13.
bolt pays on days 5, 15, 25, .... t3 on day 4 is paid on day 5, while t4 on day 5 and t6 on day 13 are paid on day 15. t7 happens before cove's first payout, so it is paid on day 14. Both day-14 lines are listed by merchant name, so acme comes before cove.
Example 2
merchants = ["zed,10,10","amy,10,10"]transactions = ["x3,100,0,zed","x2,200,3,amy","x1,300,9,zed","x4,400,10,amy"]return = ["Day 10: amy $2.00 [x2]","Day 10: zed $4.00 [x3, x1]","Day 20: amy $4.00 [x4]"]Both merchants pay on days 10, 20, ..., but their money is never combined, so day 10 has one line for each merchant. zed's IDs keep their input order, x3 before x1. x4 happens on day 10 itself, so it is paid on day 20.
Example 3
merchants = ["solo,10,5"]transactions = []return = []There are no transactions, so there are no payouts.
Constraints
1 <= merchants.length <= 10000 <= transactions.length <= 2000- Merchant names and transaction IDs contain
1to64ASCII letters, digits, or underscores. Merchant names are unique, and transaction IDs are unique. - Every merchant has exactly three comma-separated fields and every transaction has exactly four. Numbers are decimal integers without leading zeros, and each transaction's
merchantis a listed merchant name. 0 <= firstPayout <= 10^9,1 <= payoutInterval <= 10^90 <= amount <= 10^9cents and0 <= day <= 10^9- Payout days are at most
2 * 10^9, but totals can exceed2^31 - 1. Use 64-bit integers for totals.