Save 25%Black Friday: 25% off all plans — use code BLACKFRIDAY2026 at checkout. Ends Monday, Dec 7.Claim the deal

A fraud detection team wants to assess merchant fraud risk using transactions from a single day. You are given a transaction list, a corresponding rule for each transaction, and a list of merchants with their base risk scores. Your task is to compute a fraud score for every merchant.

Initialize each merchant's current score to its base score. Apply the following three steps in order, completing a separate pass over the entire transaction list for each step:

  1. Transaction Amount Multiplication:
    For a given transaction, if its amount is greater than the corresponding rule's minTransactionAmount, multiply the merchant's current score by that rule's multiplicativeFactor.

  2. Repeated Customer Additions:
    For every merchant-customer pair, once the third or subsequent transaction occurs (counting all transactions made by that customer to that merchant), add the corresponding rules' additiveFactor values to the merchant's current score cumulatively. On the third transaction, add the factors from the first, second, and third transactions together. Each later transaction adds only its own factor.

  3. Same-Hour Penalty:
    For every merchant-customer-hour group, when the third or subsequent transaction occurs within the same hour, apply the corresponding rules' penalty values as follows:

    • If the hour is between 12 and 17 (inclusive), add the penalties for the first, second, and third transactions together when the third occurs. Each subsequent transaction adds its own penalty.
    • If the hour is between 9 and 11 (inclusive) or 18 and 21 (inclusive), subtract those penalties in the same cumulative way.
    • If the hour is outside these ranges, no penalty is applied.

Within each pass, process transactions in input order. A merchant with no transactions retains its base score.

The input data contains:

  • transactionsList: Each element is a comma-separated string "<merchantId>,<amount>,<customerId>,<hour>" with the following fields:

    • merchantId: ID of the merchant receiving the payment.
    • amount: Amount of the payment (integer).
    • customerId: ID of the customer making the payment.
    • hour: Hour of the transaction (integer).
  • rulesList: Each element corresponds to a transaction in transactionsList and is a comma-separated string formatted as: "<minTransactionAmount>,<multiplicativeFactor>,<additiveFactor>,<penalty>".

    • minTransactionAmount: Transaction amount threshold for the multiplier rule (integer).
    • multiplicativeFactor: Multiplicative factor used in the rule (integer).
    • additiveFactor: Additive factor used in the rule (integer).
    • penalty: Penalty/adjustment applied if the rule conditions are met (integer).
  • merchantsList: Each element is a string formatted as: "<merchantId>,<baseScore>".

    • merchantId: ID of the merchant as mentioned above.
    • baseScore: Base fraud risk score of the merchant (integer).

Return a list of comma-separated strings, where each string is in the format "<merchantId>,<fraudScore>". The result must be ordered by merchantId in lexicographical order.

Constraints:

  • 1 ≤ transactionsList.length = rulesList.length ≤ 1000
  • 1 ≤ merchantsList.length ≤ 1000
  • 1 ≤ baseScore ≤ 50
  • 0 ≤ hour ≤ 23
  • No integer overflow will occur.
  • All merchant and customer IDs are non-empty strings. Amounts, factors, and penalties are integers.

Example 1:

Input: transactionsList = ["merchant1,1200,customer1,10",                            "merchant1,500,customer1,10",                            "merchant2,2400,customer1,15",                            "merchant1,800,customer1,16",                            "merchant1,1000,customer2,17",                            "merchant1,1400,customer1,10"],        rulesList = ["1000,2,8,15", "1400,5,3,19", "2300,3,17,3", "1800,2,9,6", "1000,4,8,2", "1200,3,11,7"],        merchantsList = ["merchant1,10", "merchant2,20"] Output: ["merchant1,50", "merchant2,60"] Explanation: The first pass multiplies merchant1's score from 10 to 20, then to 60; merchant2's score becomes 20 × 3 = 60. In the second pass, customer1's third transaction with merchant1 adds 8 + 3 + 9, and the fourth adds 11, bringing merchant1 to 91. In the third pass, customer1's three transactions with merchant1 at hour 10 subtract 15 + 19 + 7, leaving 50.

Example 2:

Input: transactionsList = ["merchant1,1200,customer1,10"], rulesList = ["1000,2,8,15"], merchantsList = ["merchant1,10"] Output: ["merchant1,20"]

Example 3:

Input: transactionsList = ["merchant1,500,customer1,10", "merchant1,500,customer1,10", "merchant1,500,customer1,10"], rulesList = ["1000,2,5,10", "1000,2,5,10", "1000,2,5,10"], merchantsList = ["merchant1,10"] Output: ["merchant1,-5"]


Accepted
2.4k/6.3k
Acceptance Rate37.7%
Test Cases
Input
transactionsList =
rulesList =
merchantsList =
Output
Expected