Stripe OA 面试真题解析:计算商户 Fraud Score 的交易规则模拟

42次阅读
没有评论

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 day
  • rules_list: a list of rules corresponding to the transactions in the same order
  • merchants_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 payment
  • amount: the payment amount
  • customer_id: the customer who makes payment
  • hour: the hour of the transaction

Each merchant in merchants_list is represented as a comma-separated string with:

  • merchant_id
  • base_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’s multiplicative_factor.
  • If the same customer_id has made three or more transactions to that merchant_id, including the current transaction, add the rule’s additive_factor to the merchant’s current score cumulatively.
  • If the transaction is the third or more from the same customer_id in the same hour for the same merchant_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"
]

这道 Stripe OA 题本质上是对商户进行逐笔交易模拟打分:先用 merchants_list 初始化每个商户的 base score,再按 transactions_list 与 rules_list 一一对应地扫描交易,依据金额阈值决定是否乘以 multiplicative factor;同时要用哈希表分别统计“同一 merchant + customer 的累计交易次数”和“同一 merchant + customer + hour 的累计交易次数”,当某个客户对同一商户的交易次数达到 3 次及以上时累计加 additive factor,而当同一小时内达到 3 次及以上时再根据时间段加或减 penalty。最终按商户 ID 字典序输出结果。题目考察的核心是字符串解析、哈希计数和按顺序的状态更新,时间复杂度可做到 O(n + m) 级别。

正文完
 0