FastPrepMerchant Payout Scheduler, Part 3: Hold Periods

Merchant Payout Scheduler, Part 3: Hold Periods

Stripe logoStripe● MediumINTERNPHONE SCREEN

Problem statement

Interview-style brief

This is Part 3 of the merchant payout scheduler. Each merchant still has its own payout calendar, and now each merchant also has a waiting period: a transaction can be paid only after it has waited at least that many days. Compute every payout.

Merchant Payout Scheduler Series

These exercises build on one another. Each page includes the rules needed to solve that part.

  1. Merchant Payout Scheduler, Part 1
  2. Merchant Payout Scheduler, Part 2: Per-Merchant Schedules
  3. Merchant Payout Scheduler, Part 3: Hold Periods (this part)

FastPrep practice contract

Each element of merchants has the format name,firstPayout,payoutInterval,holdDays. 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 the earliest payout day P of its merchant with P > d and P - d >= holdDays. Waiting exactly holdDays days is enough. A transaction is never paid on its own day, even when holdDays is 0; as in Parts 1 and 2, it waits for a later payout.

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

getHeldPayouts(merchants: String[], transactions: String[]) → String[]

Examples

Example 1

merchants = ["cove,14,100,1","acme,7,7,3","bolt,5,10,0"]transactions = ["t1,1250,2,acme","t2,800,5,acme","t3,4000,4,bolt","t4,999,5,bolt","t5,100,11,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, ... and needs a 3-day wait. t1 on day 2 has waited 5 days by day 7, so it is paid then. t2 on day 5 has waited only 2 days by day 7, so it moves to day 14. t5 on day 11 has waited exactly 3 days by day 14, which is enough.

bolt pays on days 5, 15, 25, ... with no wait. t3 on day 4 is paid on day 5, but t4 happens on day 5 itself, so it waits for day 15, together with t6. cove needs a 1-day wait, and t7 on day 10 is paid at cove's first payout, day 14. Both day-14 lines are listed by merchant name, so acme comes before cove.

Example 2

merchants = ["weekly,3,3,10","daily,1,1,0"]transactions = ["b,250,0,weekly","a,500,0,daily","c,75,3,weekly","d,1,1,daily"]return = ["Day 1: daily $5.00 [a]","Day 2: daily $0.01 [d]","Day 12: weekly $2.50 [b]","Day 15: weekly $0.75 [c]"]

weekly pays every 3 days from day 3, but each transaction must wait 10 days. b on day 0 skips days 3, 6 and 9 and is paid on day 12. c on day 3 can be paid from day 13 on, so it is paid on day 15. daily pays every day with no wait, so a on day 0 is paid on day 1 and d on day 1 is paid on day 2.

Example 3

merchants = ["solo,10,5,2"]transactions = []return = []

There are no transactions, so there are no payouts.

Constraints

  • 1 <= merchants.length <= 1000
  • 0 <= transactions.length <= 2000
  • Merchant names and transaction IDs contain 1 to 64 ASCII letters, digits, or underscores. Merchant names are unique, and transaction IDs are unique.
  • Every merchant has exactly four comma-separated fields and every transaction has exactly four. Numbers are decimal integers without leading zeros, and each transaction's merchant is a listed merchant name.
  • 0 <= firstPayout <= 10^9, 1 <= payoutInterval <= 10^9, and 0 <= holdDays <= 10^9
  • 0 <= amount <= 10^9 cents and 0 <= day <= 10^9
  • Payout days can be as large as 3 * 10^9 - 1, and totals can exceed 2^31 - 1. Use 64-bit integers.

More Stripe problems

See Stripe hiring insights
Saved
public String[] getHeldPayouts(String[] merchants, String[] transactions) {
    // Write your code here.
}
Checking account…