The fraud detection team at Stripe wants to assign a fraud score to each merchant using a sequence of transactions and per-transaction rules.
You are given three lists:
transactions_list: a list of transactions for a given dayrules_list: a list of rules corresponding to the transactions in the same ordermerchants_list: a list of merchant profiles
A transaction is represented as a comma-separated string with the following fields:
merchant_id: the merchant who receives paymentamount: the payment amountcustomer_id: the customer who makes paymenthour: the hour of the transaction
Each merchant in merchants_list is represented as a comma-separated string with:
merchant_idbase_score: the merchant’s initial fraud risk score
For each transaction and its corresponding rule, update the merchant’s score as follows:
- Start with the merchant’s
base_score. - If the transaction amount is greater than the rule’s
min_transaction_amount, multiply the merchant’s current score by the rule’smultiplicative_factor. - If the same
customer_idhas made three or more transactions to thatmerchant_id, including the current transaction, add the rule’sadditive_factorto the merchant’s current score cumulatively. - If the transaction is the third or more from the same
customer_idin the same hour for the samemerchant_id, then:- If the hour is between 12 and 17 inclusive, add the penalty each time.
- If the hour is between 9 and 11 inclusive, or 18 and 21 inclusive, subtract the penalty each time.
- If the hour falls outside those ranges, do nothing.
Return a list of comma-separated strings denoting the merchants in lexicographical order and their fraud scores.
Example input:
transactions_list = [
"merchant1,1200,customer1,10",
"merchant1,500,customer1,10",
"merchant2,2400,customer1,15",
"merchant1,800,customer1,16",
"merchant1,1000,customer2,17",
"merchant1,1400,customer1,10",
]
rules_list = [
"1000,2,8,15",
"1400,5,3,19",
"2300,3,17,3",
"1800,2,9,6",
"1000,4,8,2",
"1200,3,11,7",
]
merchants_list = [
"merchant1,10",
"merchant2,20",
]
Example output:
[
"merchant1,50",
"merchant2,60"
]
This Stripe OA problem is a transaction-simulation scoring task. Initialize each merchant with its base score, then process transactions and rules in order: multiply the current score when the amount exceeds the threshold, cumulatively add the additive factor once a customer reaches three or more transactions with the same merchant, and apply an additional time-based penalty or reward when the same customer makes three or more transactions in the same hour for that merchant. The key implementation idea is to parse the comma-separated records and maintain hash maps for merchant/customer counts and merchant/customer/hour counts. Finally, return merchant scores sorted by merchant ID.