Build a Banking System with Activity Rankings
Company: Capital One
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
# Build a Banking System with Activity Rankings
## Problem
Implement an in-memory banking system that processes these operations:
- `CREATE_ACCOUNT account_id`
- `DEPOSIT account_id amount`
- `TRANSFER source_id target_id amount`
- `TOP_ACTIVITY n`
The operation rules are:
1. Creating a new account returns `True`; creating an existing account returns `False`.
2. A successful deposit adds `amount` to the account balance and returns the new balance. It returns `-1` if the account does not exist.
3. A successful transfer subtracts `amount` from the source, adds it to the target, and returns the source's new balance. It returns `-1` if either account is missing, the account identifiers are equal, or the source lacks funds.
4. Each account has an activity indicator. A successful deposit adds its amount to that account's activity. A successful transfer adds its amount to both participating accounts' activity. Failed operations do not affect balances or activity.
5. `TOP_ACTIVITY n` returns up to `n` strings formatted as `"account_id(activity)"`, ordered by activity descending and then account identifier ascending. If fewer than `n` accounts exist, return all of them.
Implement the literal-input driver below and return one result per operation in input order. Operation arguments are strings; parse monetary amounts and `n` as integers.
```python
def process_operations(operations: list[list[str]]) -> list[object]:
...
```
## Constraints
- `1 <= len(operations) <= 100_000`
- Account identifiers are non-empty ASCII strings.
- Deposit and transfer amounts are positive integers.
- `n` is a positive integer.
- Balances and activity indicators fit in signed 64-bit integers.
## Example
```python
operations = [
["CREATE_ACCOUNT", "account1"],
["CREATE_ACCOUNT", "account2"],
["DEPOSIT", "account1", "2700"],
["TRANSFER", "account1", "account2", "200"],
["TOP_ACTIVITY", "2"],
]
```
The result is:
```python
[True, True, 2700, 2500, ["account1(2900)", "account2(200)"]]
```
Quick Answer: Implement an in-memory banking system with account creation, deposits, transfers, and activity rankings. Correctly update balances and per-account activity only on successful operations, with deterministic ranking tie-breaks.
Process account creation, deposits, transfers, and top-activity queries. Successful deposits add their amount to the account's activity; successful transfers add their amount to both participants. Return one result per string operation.
Constraints
- Account identifiers are nonempty strings.
- Amounts and TOP_ACTIVITY n are positive canonical integer strings.
- Failed operations do not affect balances or activity.
- Transfers require distinct existing accounts and sufficient funds.
- Rank by activity descending, then account identifier ascending.
Examples
Input: ([["CREATE_ACCOUNT", "account1"], ["CREATE_ACCOUNT", "account2"], ["DEPOSIT", "account1", "2700"], ["TRANSFER", "account1", "account2", "200"], ["TOP_ACTIVITY", "2"]],)
Expected Output: [True, True, 2700, 2500, ["account1(2900)", "account2(200)"]]
Explanation: The prompt example tracks deposits and both sides of a transfer.
Input: ([["CREATE_ACCOUNT", "a"], ["CREATE_ACCOUNT", "a"], ["TOP_ACTIVITY", "1"]],)
Expected Output: [True, False, ["a(0)"]]
Explanation: Duplicate creation fails and leaves the original account.
Hints
- Store balance and activity together for each account.
- Validate every transfer condition before changing either account.
- Generate ranking strings only for TOP_ACTIVITY.