Implement a Timestamped Banking System
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Take-home Project
Implement an in-memory banking system that processes API calls in nondecreasing `timestamp` order.
Support the following operations:
- `create_account(timestamp, account_id) -> bool`
- Create a new account with balance `0`.
- Return `false` if the account already exists; otherwise return `true`.
- `deposit(timestamp, account_id, amount) -> int | None`
- Add `amount` to the account balance.
- Return the updated balance.
- Return `None` if the account does not exist.
- `transfer(timestamp, source, target, amount) -> int | None`
- Move `amount` from `source` to `target`.
- Return the updated balance of `source`.
- Return `None` if either account does not exist, `source == target`, or `source` has insufficient funds.
- `top_spenders(timestamp, n) -> list[str]`
- Return up to `n` accounts with the largest total outgoing amount.
- Format each result as `"account_id(total_outgoing)"`.
- Sort by total outgoing amount in descending order; break ties by `account_id` in ascending lexicographic order.
- Outgoing amount includes money successfully sent out of an account.
- `schedule_payment(timestamp, account_id, amount, delay) -> str | None`
- Schedule a withdrawal of `amount` from `account_id` at time `timestamp + delay`.
- Return a globally unique payment ID such as `"payment1"`, `"payment2"`, and so on.
- Return `None` if the account does not exist.
- `cancel_payment(timestamp, account_id, payment_id) -> bool`
- Cancel a pending scheduled payment.
- Return `true` only if the payment exists, belongs to `account_id`, and has not already executed or been canceled.
- `merge_accounts(timestamp, account_id_1, account_id_2) -> bool`
- Merge `account_id_2` into `account_id_1`.
- The surviving account keeps `account_id_1`.
- Its balance becomes the sum of both balances.
- Its outgoing total becomes the sum of both outgoing totals.
- Any pending scheduled payments owned by `account_id_2` are transferred to `account_id_1`.
- `account_id_2` is then deleted.
- Return `false` if either account does not exist or the two IDs are the same.
- `get_balance(timestamp, account_id, time_at) -> int | None`
- Return the balance of `account_id` at historical time `time_at`.
- Return `None` if the account does not exist at query time or if `time_at` is earlier than the account's creation time.
Additional assumptions to make the problem self-contained:
- All amounts are nonnegative integers.
- Whenever an operation is called at time `t`, all scheduled payments due at or before `t` must be processed before the operation itself.
- If multiple scheduled payments are due at the same time, process them in payment creation order.
- A scheduled payment executes only if the owning account has sufficient funds at execution time; otherwise it is skipped.
Quick Answer: This question evaluates the ability to design and implement an in-memory, time-ordered stateful system handling event scheduling, transactional updates, historical queries, merging state, and ranked aggregations using appropriate data structures and algorithms, and it belongs to the Coding & Algorithms domain.