Coinbase OA 面试真题解析:删除第 N 笔交易后计算账户余额

17次阅读
没有评论

Given a set of transactions between users and an initial balance per user, output the final balance per user. Note that each transaction denotes the payer->payee relation, and the amount paid is expressed as a percentage.

For example, suppose a given transaction is A->B 20%. This means A paid 20% of whatever he/she had to B.

Introduce an ability to delete a transaction given the user and the transaction’s number with respect to that user. User Y wants to delete their nth transaction. What is the balance of user X?

Example:

User B wants to delete their 2nd transaction (B->D 30%). Note: deletion is by nth transaction with respect to a user.

What is the balance of C?

Input from Level 1:

A has $100

List of transactions:

A->B 20%
A->C 40%
B->D 30%
C->D 50%
C->F 20%
B->E 50%
D->E 25%
F->G 10%
E->C 50%

Execution time limit: 4 seconds (py3)

这道题的核心是模拟一张“转账百分比网络”:每笔交易 A->B x% 表示 A 将自己当前余额的 x% 转给 B,因此每个用户的最终余额依赖于交易顺序和前序交易带来的余额变化。做题时通常需要按用户维护交易列表,并在计算时逐笔更新余额;题目进一步加入“删除某个用户的第 n 笔交易”的操作,因此需要能按用户定位交易并在删除后重新计算受影响的资金流。常见思路是用哈希表存每个用户的当前余额、再用按用户分组的交易序列来支持删除和重算,重点是处理好“百分比基于当时余额”这一规则。

正文完
 0