Design a bank system with scheduled transfers
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Take-home Project
## Bank System (OOD): Accounts, Transfers, Scheduling, Top Spending, Merge
Design an in-memory banking system that supports:
- account creation
- deposits/withdrawals
- immediate transfers
- scheduled (delayed) transfers with cancellation
- querying top spenders
- merging accounts
### Core rules
- Each account has a balance (integer, starts at 0).
- Each operation is called with a `timestamp`.
- Before executing any operation at time `t`, the system must **process all scheduled transfers** whose execution time `<= t`.
- “Spending” for a customer is the total money sent out (withdrawals + transfers out, including scheduled transfers once executed; depending on your interpretation, you may also count scheduled transfers as spending when reserved—state your choice and be consistent).
### API
1. `create_account(timestamp, customer_id) -> bool`
- Create if not exists.
2. `deposit(timestamp, customer_id, amount) -> bool`
- Fail if account doesn’t exist.
3. `withdraw(timestamp, customer_id, amount) -> bool`
- Fail if account doesn’t exist or insufficient funds.
- Counts toward spending.
4. `transfer(timestamp, from_id, to_id, amount) -> bool`
- Immediate transfer.
- Fail if either account missing or insufficient funds.
- Counts `amount` toward `from_id` spending.
5. `top_spending(timestamp, n) -> list<string>`
- Return top `n` customers formatted `"customerId(total)"`.
- Sort by total spending desc, then customerId asc.
6. `schedule_transfer(timestamp, from_id, to_id, amount, delay) -> string | null`
- Create a transfer that will execute at `timestamp + delay`.
- If invalid (missing accounts or insufficient funds), return `null`.
- Return a unique `transfer_id` for cancellation.
- Funds may be “reserved” immediately or deducted at execution; choose one and document it.
7. `cancel_transfer(timestamp, customer_id, transfer_id) -> bool`
- Cancel only if:
- `transfer_id` exists
- status is still pending (not executed)
- the `customer_id` is the sender
- Return whether cancellation succeeded.
8. `merge_account(timestamp, from_id, to_id) -> bool`
- Merge `from_id` into `to_id`:
- Move remaining balance into `to_id`
- Add spending totals
- Update any **pending** scheduled transfers that reference `from_id` as sender or receiver to reference `to_id`
- Delete `from_id`
### Notes
- Assume up to ~1e5 operations.
- You may use a min-heap/priority queue for scheduled transfers.
Quick Answer: This question evaluates object-oriented design and stateful system skills, including time-ordered event processing, scheduling and cancellation semantics, balance accounting, and top-k aggregation using appropriate data structures.