Implement a simplified version of a banking system.
Plan your design according to the level specifications below:
- Level 1: The banking system should support creating new accounts and depositing money into and withdrawing/paying money from accounts.
- Level 2: The banking system should support ranking accounts based on the total value of transactions.
- Level 3: The banking system should support scheduling transfers and checking the scheduled transfer status.
- Level 4: The banking system should support merging two accounts while retaining the balances and transaction histories of the original accounts.
To move to the next level, you should pass all the tests at the current level.
Note: All queries will have a timestamp parameter, a stringified timestamp in milliseconds. It is guaranteed that all timestamps are unique and are in a range from 1 to 10^9. Queries will be given in the order of strictly increasing timestamps.
The following operations are supported:
create_account(timestamp: int, account_id: str) -> bool
Should create a new account with the given account_id.
deposit(timestamp: int, account_id: str, amount: int) -> int | None
Should deposit the given amount of money into the account.
pay(timestamp: int, account_id: str, amount: int) -> int | None
Should withdraw/pay the given amount of money from the account.
top_activity(timestamp: int, n: int) -> list[str]
Should return the top n accounts with the highest total value of transactions sorted in descending order. In case of ties, sort alphabetically by account_id in ascending order. The returned value should be a list of strings in the format ["<account_id_1>(<transactions_value_1>)", ...].
transfer(timestamp: int, source_account_id: str, target_account_id: str, amount: int) -> str | None
Should initiate a transfer between accounts. The amount is withdrawn from the source account and held until accepted by the target account or until the transfer expires. The transfer expires after 24 hours, which is equal to 24 * 60 * 60 * 1000 = 86400000 milliseconds. A transfer expires at the beginning of the next millisecond after the expiration period ends.
accept_transfer(timestamp: int, account_id: str, transfer_id: str) -> bool
Should accept the transfer with the given transfer_id.
merge_accounts(timestamp: int, account_id_1: str, account_id_2: str) -> bool
Should merge account_id_2 into account_id_1.
get_balance(timestamp: int, account_id: str, time_at: int) -> int | None
Should return the total amount of money in account_id at the given time_at.
If the specified account did not exist at time_at, return None.
If a query has been processed at time_at, get_balance must reflect the account balance after the query has been processed.
这道 Meta OA 题要求你设计一个分阶段演进的银行系统,核心是围绕“账户状态、交易总额、定时转账、合并账户和历史余额查询”建立一套统一的数据模型。实现时通常需要用哈希表维护账户信息,用时间线或事件日志记录每次存取款、转账接受 / 过期、合并带来的余额迁移,并额外保存每个账户的交易累计值以支持 top_activity 的排序;而 get_balance 则要能根据时间点回看某个账户在指定时刻的真实余额,注意同一 timestamp 的查询要返回该时刻操作完成后的状态。Level 3 的 transfer/accept_transfer 关键在于把转账分成“冻结”和“确认”两个阶段,Level 4 的 merge_accounts 则要处理旧账户失效、未完成转账重定向以及交易历史合并。整体是一个典型的状态管理与事件模拟题。