Merchant Fraud Risk Scoring
Learn this problemProblem statement
You are given the transactions for one day, a list of merchants with their starting fraud-risk scores, and one rule row corresponding to each transaction. Compute the final fraud-risk score of every merchant.
Input representation
transactions[i] = [customerId, merchantId, hour, amount], wherehourandamountare base-10 strings.merchantIds[j]identifies the merchant whose initial score isbaseScores[j].rules[i] = [minTransactionAmount, multiplicativeFactor, additiveFactor, penalty]applies totransactions[i].
Initialize each merchant's currentScore to its base score. Then perform the following three passes in order. Each pass scans the complete transaction list from left to right.
- Amount pass: If a transaction's amount is strictly greater than its rule's
minTransactionAmount, multiply that merchant'scurrentScoreby the rule'smultiplicativeFactor. - Repeat-customer pass: Track transactions by the pair
(customerId, merchantId). When a pair reaches its third transaction, add theadditiveFactorvalues of its first, second, and third transactions to that merchant's score. For every later transaction of the same pair, add only that transaction'sadditiveFactor. - Same-hour pass: Track transactions by
(customerId, merchantId, hour). For hours from12through17, inclusive, when a group reaches its third transaction, add thepenaltyvalues of its first, second, and third transactions to that merchant's score. For every later transaction in the same group, add only that transaction'spenalty. In this practice version, hours outside that range do not apply a same-hour penalty.
Return the final scores in the same order as merchantIds.
Function
calculateMerchantFraudScores(transactions: String[][], merchantIds: String[], baseScores: long[], rules: long[][]) β long[]Examples
Example 1
transactions = [["c1","m1","13","120"],["c1","m1","13","80"],["c1","m1","13","90"],["c2","m2","9","50"]]merchantIds = ["m1","m2"]baseScores = [10,5]rules = [[100,2,3,4],[100,3,5,6],[100,2,7,8],[40,2,1,9]]return = [53,10]During the amount pass, m1 changes from 10 to 20, and m2 changes from 5 to 10.
The three (c1, m1) transactions add 3 + 5 + 7 = 15. Because all three also occur during hour 13, their penalties add another 4 + 6 + 8 = 18. The final scores are [20 + 15 + 18, 10] = [53, 10].
Example 2
transactions = [["c","m","10","50"],["c","m","10","50"],["c","m","10","50"]]merchantIds = ["m"]baseScores = [4]rules = [[100,2,1,10],[100,2,2,20],[100,2,3,30]]return = [10]No amount exceeds its threshold. The third transaction activates the repeat-customer rule, adding 1 + 2 + 3 = 6. Hour 10 is outside the midday range used by this practice version, so no same-hour penalty is added.
Example 3
transactions = [["c","m","12","101"],["c","m","12","101"],["c","m","12","101"],["c","m","12","101"]]merchantIds = ["m"]baseScores = [2]rules = [[100,2,1,1],[100,3,1,2],[100,2,1,3],[100,2,5,4]]return = [66]The amount pass changes the score from 2 to 48. The repeat-customer pass adds 1 + 1 + 1 when the third transaction is reached and then adds 5 for the fourth. The same-hour pass adds 1 + 2 + 3 at the third transaction and 4 at the fourth, giving 48 + 8 + 10 = 66.
Constraints
1 <= transactions.length <= 200000transactions.length == rules.length- Every
transactions[i]has exactly four fields, and everyrules[i]has exactly four values. 1 <= merchantIds.length == baseScores.length <= 200000- Merchant IDs are unique, and every transaction references a merchant in
merchantIds. - Customer and merchant IDs are non-empty ASCII strings of length at most
40. 0 <= hour <= 230 <= amount, minTransactionAmount <= 10^91 <= multiplicativeFactor <= 10^60 <= baseScores[i], additiveFactor, penalty <= 10^9- Every intermediate and final score fits in a signed 64-bit integer.
More Stripe problems
- Group Linked Merchant RecordsPHONE SCREEN Β· Seen Jul 2026
- Invoice / Payment ReconciliationPHONE SCREEN Β· Seen Jul 2026
- Incident MonitorOA Β· Seen Jul 2026
- WebSocket Load Balancer β Basic Load Balancing (Part 1)OA Β· Seen Jul 2026
- Rule-Driven Transaction Fraud DetectionOA Β· PHONE SCREEN Β· Seen Jul 2026
- Deployment Window SchedulerOA Β· Seen Jul 2026
- Directly Linked UsersOA Β· Seen Jun 2026
- Fraud Ring SizeOA Β· Seen Jun 2026