Split Tender Part 1 - Basic Split Tendering
Problem statement
Many of Stripe's merchants rely on Stripe Terminal to accept in-person payments at the point of sale. In physical retail, it's common for a customer to cover a purchase with multiple payment methods. For example, they might cover a $100 purchase with a $40 gift card combined with a $60 credit card. This is called a split-tender transaction, and supporting it is a key requirement for retail merchants on Terminal.
For this multi-part question, we will be writing a system to process split-tender payments. When a transaction occurs, the system first deducts from the customer's available gift card balance, then charges the remaining balance to the customer's credit card. Given a chronological stream of payment events, your task is to calculate the correct credit card charge for each transaction. The question is broken down into four parts, each introducing a new event type: gift card expiration, regional credit card surcharges, and complex refund logic.
This exercise: Implement Part 1. Process gift-card additions and basic charges only; ignore expiration and ZIP codes.
Split Tender Series
These four exercises build on one another. Each page includes the rules needed to solve that part.
- Split Tender Part 1 - Basic Split Tendering (this part)
- Split Tender Part 2 - Expiration Dates
- Split Tender Part 3 - Surcharges
- Split Tender Part 4 - Refunds
The function parameters below are shared by the four-part source. This standalone exercise uses only the event types and parameters listed in its practice interface.
Source-Wide Function Parameters
string events[n]: lines for each event in chronological order. Lines have a format defined by one of three schemas:
timestamp,ADD_GIFTCARD,customer_id,card_id,amount,valid_beforetimestamp,CHARGE,charge_id,customer_id,amount,zip_codetimestamp,REFUND,charge_id,refund_amount
string surcharge_rates[m]: a list of non-overlapping zip code ranges mapped to a surcharge percentage in the format start_zip-end_zip:rate, where start and end are inclusive. Example: 90000-99999:0.05 (representing 5%).
General Constraints and Assumptions
- Events are in chronological order by timestamp.
- All event lines are well-formed.
charge_idvalues are unique acrossCHARGEevents.REFUNDevents always reference a priorCHARGEevent.- Each
charge_idwill receive at most oneREFUNDevent. card_idvalues fromADD_GIFTCARDevents are unique.surcharge_ratescontains non-overlapping, valid zip code ranges.zip_codevalues are five-digit numeric strings.
Monetary Representation and Precision
- All monetary amounts in the input and output are represented as non-negative integer numbers of cents.
- For example,
$40.00is represented as4000. - Surcharge rates are decimal fractions; for example,
0.05represents5%. - Each surcharge calculation results in a whole number. You don't need to round.
Returns: string[n]: an array of strings, one per input event, in the same order.
- For
CHARGEevents, return the formatted charge string using the schemaCC_CHARGE_AMOUNT,GIFT_CARD_ID_1:AMOUNT_USED,GIFT_CARD_ID_2:AMOUNT_USED. - For all other event types, return an empty string
""unless a later part specifies otherwise. - The length of the output array must exactly match the length of the input events array.
Part 1: Basic split tendering
For Part 1, process ADD_GIFTCARD and CHARGE events. All customer balances start at 0.
When an ADD_GIFTCARD event is received, add the specified amount to the designated customer's total gift card balance. Return an empty string "".
When a CHARGE event is received, attempt to cover the transaction cost using the customer's available gift card balance first.
- If the gift card balance is greater than or equal to the charge amount, deduct the charge amount from their gift card balance. The credit card charge is 0.
- If the charge amount exceeds the gift card balance, drain the entire gift card balance to 0. The remaining unpaid amount becomes the credit card charge.
Output Format for CHARGE: Return a string starting with the credit card charge, followed by a comma-separated list of the gift cards debited and their amounts (CARD_ID:AMOUNT). If no gift cards were debited, output only the credit card charge.
CC_CHARGE_AMOUNT,GIFT_CARD_ID_1:AMOUNT_USED,GIFT_CARD_ID_2:AMOUNT_USED
Note: In Part 1, each customer will hold at most one active gift card at a time. You do not need to implement card prioritization or sorting logic until Part 2.
Example Input
Suppose we receive the following events in order:
events = [
"1,ADD_GIFTCARD,cust1,gc1,5000,10", # cust1 gift card gc1 with 5000 cents ($50.00), expiration: t=10
"2,ADD_GIFTCARD,cust2,gc2,2000,15",
"3,CHARGE,chg1,cust2,3000,10000", # cust2 is charged 3000
"4,CHARGE,chg2,cust1,3000,10000"
]
Example Output
[
"", # ADD_GIFTCARD always returns an empty string
"",
"1000,gc2:2000", # gc2 drained for 2000, remaining 1000 goes to the credit card
"0,gc1:3000" # gc1 covers the full 3000, so the credit card charge is 0
]
Practice Details
Complete the function process_events in the editor below.
In this practice interface, the method is named processEvents. The only parameter is events; this part does not use the source-wide surcharge-rate input. The valid_before and zip_code fields are present but ignored in Part 1.
Function
processEvents(events: String[]) → String[]Examples
Example 1
events = ["1,ADD_GIFTCARD,cust1,gc1,5000,10","2,ADD_GIFTCARD,cust2,gc2,2000,15","3,CHARGE,chg1,cust2,3000,10000","4,CHARGE,chg2,cust1,3000,10000"]return = ["","","1000,gc2:2000","0,gc1:3000"]The first purchase drains gc2 and charges 1000 cents to the credit card. The second consumes 3000 cents from gc1.
Example 2
events = ["1,CHARGE,c1,alice,1250,94105"]return = ["1250"]No gift card is available, so all 1250 cents go to the credit card.
Example 3
events = ["1,ADD_GIFTCARD,alice,g1,2550,50","2,CHARGE,c1,alice,1025,10000","3,CHARGE,c2,alice,2000,10000"]return = ["","0,g1:1025","475,g1:1525"]The first charge leaves 1525 cents. The second drains that balance and charges the remaining 475 cents to the credit card.
Constraints
- For this exercise, assume there are between
1and10^5events. - For this exercise, assume events with equal timestamps are processed in input order.
- For this exercise, assume all input customer, card and charge IDs contain ASCII letters, digits or underscores, have length from
1to64, and contain no commas. - For this exercise, assume each input monetary amount is at most
10^9cents, and all running balances and intermediate money arithmetic fit signed 64-bit integers. - For this exercise, assume a card is active while it has a positive balance; a zero-value gift-card addition does not replace an existing active card.
- This part: Only
ADD_GIFTCARDandCHARGEevents appear. In Part 1, each customer will hold at most one active gift card at a time; expiration and ZIP codes do not affect this part.