Stripe OA 面试真题解析:Receivables Aggregation 聚合与合约更新

17次阅读
没有评论

Stripe in Brazil is obliged to register customer transactions for each merchant with the central bank as an aggregated unit per day. These are called receivables. A receivable is identified by 3 identifiers:

  • merchant_id (String): The id of the merchant on Stripe side.
  • card_type (String): The type of the card used for the transaction (e.g. Visa)
  • payout_date (String): String date of the funds available to the merchant by Stripe.

A payment transaction in Stripe API can be represented as the following object:

Transaction {
  string customer_id
  string merchant_id
  string payout_date
  string card_type
  int amount
}

Implement register_receivables function that takes a string in CSV format where each line represents a transaction and returns the registered aggregated receivables using the rules above.

Print the returned receivables to console using the format below. Feel free to parse the CSV using a standard or a 3rd party library or implement it yourself.

You can assume the following about the input:

  • The first line of the input is a header. The header is always the same so it can be ignored or hardcoded.
  • You can assume that the input has already been read from a file and checked for correctness.
  • No data fields in this file will include commas or whitespace surrounding the field values.

You can also assume the following about the output:

  • The first line of the output is the header. The header is always the same so it can be hardcoded.
  • Order of the output does not matter.

Example input 1:

customer_id,merchant_id,payout_date,card_type,amount
cust1,merchantA,2021-12-30,Visa,150
cust2,merchantA,2021-12-30,Visa,200
cust3,merchantB,2021-12-31,MasterCard,300
cust4,merchantA,2021-12-30,Visa,50

Output 1:

id,card_type,payout_date,amount
merchantA,Visa,2021-12-30,400
merchantB,MasterCard,2021-12-31,300

Example input 2:

customer_id,merchant_id,payout_date,card_type,amount
cust1,merchantA,2021-12-29,MasterCard,50
cust2,merchantA,2021-12-29,Visa,150
cust3,merchantB,2021-12-31,Visa,300
cust4,merchantB,2021-12-29,MasterCard,200

Output 2:

id,card_type,payout_date,amount
merchantA,MasterCard,2021-12-29,50
merchantA,Visa,2021-12-29,150
merchantB,Visa,2021-12-31,300
merchantB,MasterCard,2021-12-29,200

In addition, it’s possible for merchants to sell part of their receivables and/or receive multiple contracts for a receivable. Update the registered_receivables function implemented in part 2 to respect the rules mentioned above:

  • The amount in the contracts won’t be more than it’s registered on the receivable.
  • If a receivable is sold fully to one or more contracts, it should be returned as part of the registered receivables with 0 amounts.
  • Contracts won’t have amounts equal to 0.

这道 Stripe OA 题的核心是按 <code>merchant_id</code>、<code>card_type</code> 和 <code>payout_date</code> 三个字段对交易做分组聚合,先把同一受理方、同一卡种、同一到账日的交易金额累加,生成 receivables;第二部分再考虑合同分拆与部分出售的情况,需要在原有聚合结果上继续扣减已售出的金额,若某个 receivable 被全部卖出,则仍要保留这条记录但金额变为 0。实现上通常用哈希表 / 字典作为键值映射来累计金额,并在处理合约时按同一三元组定位并递减余额,注意输出顺序不要求固定。

正文完
 0