Problem Β· Hash Table

Merchant Fraud Risk Scoring

Learn this problem
● MediumStripe logoStripeNEW GRADOA
See Stripe hiring insights

Problem 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], where hour and amount are base-10 strings.
  • merchantIds[j] identifies the merchant whose initial score is baseScores[j].
  • rules[i] = [minTransactionAmount, multiplicativeFactor, additiveFactor, penalty] applies to transactions[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.

  1. Amount pass: If a transaction's amount is strictly greater than its rule's minTransactionAmount, multiply that merchant's currentScore by the rule's multiplicativeFactor.
  2. Repeat-customer pass: Track transactions by the pair (customerId, merchantId). When a pair reaches its third transaction, add the additiveFactor values of its first, second, and third transactions to that merchant's score. For every later transaction of the same pair, add only that transaction's additiveFactor.
  3. Same-hour pass: Track transactions by (customerId, merchantId, hour). For hours from 12 through 17, inclusive, when a group reaches its third transaction, add the penalty values of its first, second, and third transactions to that merchant's score. For every later transaction in the same group, add only that transaction's penalty. 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 <= 200000
  • transactions.length == rules.length
  • Every transactions[i] has exactly four fields, and every rules[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 <= 23
  • 0 <= amount, minTransactionAmount <= 10^9
  • 1 <= multiplicativeFactor <= 10^6
  • 0 <= baseScores[i], additiveFactor, penalty <= 10^9
  • Every intermediate and final score fits in a signed 64-bit integer.

More Stripe problems

drafts saved locally
public long[] calculateMerchantFraudScores(String[][] transactions, String[] merchantIds, long[] baseScores, long[][] rules) {
    // Write your code here.
}
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]]
expected[53,10]
checking account