Banking System in Four Levels: Transfers, Top Spenders, Cashback, Merges, Past Balances
Company: Airbnb
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: hard
Interview Round: Online Assessment
Implement an in-memory banking system as the class `BankingSystemImpl` in `banking_system_impl.py`, a subclass of a provided `BankingSystem` interface. The assessment is progressive: it has four levels, unit tests check each level, and every level builds on the earlier ones, so the structure you choose at Level 1 has to absorb the later requirements. The parts below follow the levels. Levels 1 to 3 are as reported; Level 4 follows the most widely shared version of this assessment, and the version you receive may differ in details.
### Constraints and Clarifications
- Every method takes `timestamp`, an integer number of milliseconds. Assume calls arrive in strictly increasing timestamp order.
- Amounts are positive integers.
- Failures are reported through return values (`None` or `False`, as specified); methods do not raise.
- An account's outgoing total is the sum of its successful transfers out and, from Level 3, its successful payments.
### Clarifying Questions
- After an account has been merged away, may its id be created again? If so, should a past-balance query for that id still return the old account's balances for times before the merge?
- May a past-balance query ask about a time later than the call's own timestamp?
### Part 1: Accounts, deposits and transfers
```python
def create_account(self, timestamp: int, account_id: str) -> bool
def deposit(self, timestamp: int, account_id: str, amount: int) -> int | None
def transfer(self, timestamp: int, source_account_id: str,
target_account_id: str, amount: int) -> int | None
```
- `create_account` creates an account with balance 0 and returns `True`. It returns `False` if the account already exists.
- `deposit` adds `amount` to the account and returns the new balance, or `None` if the account does not exist.
- `transfer` moves `amount` from the source to the target and returns the source's new balance. It returns `None` if the source does not exist, the target does not exist, the two are the same account, or the source's balance is less than `amount`.
```hint Fail before you change anything
List every way a transfer can fail, and check all of them before any balance moves.
```
#### What This Part Should Cover
- Exact return values for every success and failure case.
- No partial update when a transfer fails.
- A per-account record that later levels can extend.
### Part 2: Top spenders
```python
def top_spenders(self, timestamp: int, n: int) -> list[str]
```
Return the `n` accounts with the largest outgoing totals, each formatted as `account_id(outgoing)`. Order by outgoing total, descending, and break ties by account id in ascending lexicographic order. If fewer than `n` accounts exist, return all of them. For example, if `account1`, `account2` and `account3` have outgoing totals 200, 500 and 500, `top_spenders(timestamp, 2)` returns `["account2(500)", "account3(500)"]`.
```hint One ordering, two directions
Work out how a single sort key can put large totals first while ids stay in alphabetical order.
```
#### What This Part Should Cover
- Correct ordering and tie-breaking.
- The exact output format.
- The cost of ranking as the number of accounts grows.
### Part 3: Payments with cashback
```python
def pay(self, timestamp: int, account_id: str, amount: int) -> str | None
def get_payment_status(self, timestamp: int, account_id: str,
payment: str) -> str | None
```
- `pay` withdraws `amount` from the account immediately and schedules a cashback of 2% of `amount`, rounded down, to be credited 24 hours later (86,400,000 ms after `timestamp`). It returns a payment id: `"payment1"`, `"payment2"` and so on. One counter is shared by all accounts, and it advances only on a successful payment.
- `pay` returns `None` if the account does not exist or its balance is less than `amount`.
- A successful payment adds `amount` to the account's outgoing total. The cashback does not reduce it.
- When a cashback falls due at the same timestamp as another operation, the cashback is credited first.
- `get_payment_status` returns `"IN_PROGRESS"` before the cashback is credited and `"CASHBACK_RECEIVED"` after. It returns `None` if the account does not exist, the payment does not exist, or the payment belongs to another account.
Example, on a new system:
```text
create_account(1, "account1") -> True
deposit(2, "account1", 1000) -> 1000
pay(3, "account1", 250) -> "payment1" (cashback 5, due at 86400003)
get_payment_status(4, "account1", "payment1") -> "IN_PROGRESS"
deposit(86400003, "account1", 100) -> 855 (750, plus the cashback of 5, plus 100)
get_payment_status(86400004, "account1", "payment1") -> "CASHBACK_RECEIVED"
```
```hint Time passes between calls
Nothing in the class runs by itself when 24 hours pass. Decide what every method must do before its own work, and how it finds every cashback that has come due.
```
#### What This Part Should Cover
- Crediting every due cashback before each operation, in due order, including several at once and one due at the current timestamp.
- Payment ids from one global counter that advances only on success.
- Ownership checks in the status query.
- Outgoing totals that include payments and ignore cashback.
### Part 4: Merging accounts and past balances
```python
def merge_accounts(self, timestamp: int, account_id_1: str,
account_id_2: str) -> bool
def get_balance(self, timestamp: int, account_id: str,
time_at: int) -> int | None
```
- `merge_accounts` merges `account_id_2` into `account_id_1` and returns `True`. It returns `False` if the ids are the same or either account does not exist.
- After a merge, account 1 holds both balances and both outgoing totals. Every payment account 2 made now belongs to account 1: cashbacks still pending are credited to account 1 when they fall due, and account 1 can query their status. Account 2 is removed, so later calls that name it behave as for a missing account.
- `get_balance` returns the account's balance at `time_at`: the balance after every change at or before `time_at`, with each cashback counted at its due time. It returns `None` if the account did not exist at `time_at`, because it had not been created yet or had already been merged away (an account merged at time `t` no longer exists at `t` or later).
- Account 1's balance before the merge is its own balance alone. Account 2's balances before the merge can still be queried.
Example, on a new system:
```text
create_account(1, "account1") -> True
create_account(2, "account2") -> True
deposit(3, "account1", 1000) -> 1000
deposit(4, "account2", 700) -> 700
pay(5, "account1", 200) -> "payment1" (cashback 4, due at 86400005)
merge_accounts(10, "account1", "account2") -> True
get_balance(11, "account1", 5) -> 800
get_balance(12, "account2", 9) -> 700
get_balance(13, "account2", 10) -> None
get_balance(86400005, "account1", 86400005) -> 1504
```
```hint What the absorbed account owns
List everything attached to account 2, including payments whose cashback has not arrived yet, and decide how each one moves.
```
```hint Answering questions about the past
A balance in the past can only come from a record kept as balances change. Decide when to write it and how to search it.
```
#### What This Part Should Cover
- A merge that moves balance, outgoing total, payment ownership and pending cashback.
- A balance history written at every change, with cashback at its due time.
- Correct `None` results before creation and after a merge.
- The cost of a merge and of a lookup as history grows.
### What a Strong Answer Covers
- A Level 1 data model that later levels extend without rewrites.
- One place that settles due cashbacks before any operation reads or changes state.
- Exact return values for every failure case across all levels.
- Time and memory costs per operation, with a faster option wherever a naive scan is slow.
### Follow-up Questions
- Another version of this assessment replaces Level 3 with payments scheduled for a later time that can be cancelled before they run. How would your design support scheduling and cancelling?
- `top_spenders` is called far more often than money moves. How would you avoid sorting every account on each call?
- Accounts can be merged again and again, so one account can end up owning payments from many others. What does each merge cost in your design, and how could you make it cheaper?
- How would you test the ordering rules (a cashback due at the same timestamp as another operation, several cashbacks falling due before one call) so a regression is caught?
Overview: Implement a banking system class over four progressive levels: accounts, deposits and transfers, a top spenders ranking, payments with a 2% cashback credited 24 hours later, account merges and balance lookups at past times. It tests data modeling, processing timed events with a min-heap, state that survives merges and code that extends cleanly.
Read the full Airbnb Software Engineer interview experience this question came from