Reddit 面试真题:根据交易日志重建 BillingStatus – 面试真题 – 一亩三分地 – OA 代写 – VO 辅助

这是一个非常贴近业务的日志恢复问题:数据库中保存的广告主账单状态丢失了,现在需要根据历史交易日志重新构建每个用户的 BillingStatus

We accidentally dropped the database where we store the current billing
status for our advertisers. Fortunately, we still have the logs for
all the transactions they did, and we can use this to recreate the
dropped data.

You are asked to process the financial transactions from the old system
to build up a BillingStatus per user to be stored in our new system.
You should create a class called BillingStatus, which will represent an
account state. Each financial transaction represents a modification
to a BillingStatus. A BillingStatus should be able to ingest new
transactions that are generated in our own systems.

Given a collection of financial transactions, we want to generate a
BillingStatus instance for each user. This can be represented as a dict:
{ user_id: BillingStatus(), user_id2: BillingStatus() }

Our BillingStatus class should start with two monetary columns:
'ad_delivery_pennies': 0, 'payment_pennies': 0

Each transaction can have multiple monetary columns. Upon processing a
transaction, the values in the monetary columns should be added to the
current value in the BillingStatus.
monetary_columns = ('ad_delivery_pennies', 'payment_pennies')

transactions = {
'ff8bc1c2-8d45-11e9-bc42-526af7764f64': {
'user_id': 1,
'ad_delivery_pennies': 1000,
'transaction_timestamp': 1500000001
},
'ff8bc2e4-8d45-11e9-bc42-526af7764f64': {
'user_id': 1,
'ad_delivery_pennies': 1000,
'transaction_timestamp': 1500000002
},
'ff8bc4ec-8d45-11e9-bc42-526af7764f64': {
'user_id': 1,
'payment_pennies': 500,
'transaction_timestamp': 1500000003
},
'fv24z4ec-8d45-11e9-bc42-526af7764f64': {
'user_id': 1,
'ad_delivery_pennies': 1000,
'payment_pennies': 500,
'transaction_timestamp': 1500000004
}
}

Expected Output:

{
1: BillingStatus(ad_delivery_pennies=3000, payment_pennies=1000)
}

重点是把每个用户的交易按 user_id 聚合起来。每个用户对应一个 BillingStatus 对象,初始时 ad_delivery_penniespayment_pennies 都是 0。遍历交易时,如果交易中出现了某个 monetary column,就把它累加到对应用户的账单状态中。

例如示例中,用户 1 一共有三笔 ad_delivery_pennies = 1000,所以最后是 3000;同时有两笔 payment_pennies = 500,所以最后是 1000。

面试中比较容易出错的地方是:不要把 transaction_timestamp 当作金额字段处理;不要假设每条交易都包含所有金额字段;也不要只维护一个全局状态,因为题目要求的是每个用户一个 BillingStatus

这类题更像真实后端业务开发。面试官主要看候选人能不能把需求读清楚,把状态封装成 class,并且写出可扩展的处理逻辑。

我们也有代面试,面试辅助,OA代写等服务助您早日上岸~

Leave a Reply

Your email address will not be published. Required fields are marked *