Katkat

Design a Banking Component with Transfers and Activity Rankings

Capital OneSoftware Engineer · Onsite

Last updated

Design an in-memory banking service supporting account creation, deposits, atomic transfers, and activity rankings driven by a string-based command interface.

Problem Statement

Design an in-memory banking engine that handles account lifecycle management, deposits, atomic peer-to-peer transfers, and activity rankings. You will implement a class BankSystem along with a command parser process_transactions(commands: list[str]) -> list[str] to execute queries sequentially.

Commands to Support

  1. CREATE

    • Creates a new account with a balance of 0 and cumulative outgoing transfer volume of 0.
    • Returns "true" if successful, or "false" if account_id already exists.
  2. DEPOSIT

    • Deposits positive integer amount into account_id.
    • Returns the updated balance as a string if successful.
    • Returns "" (empty string) if the account does not exist or amount
    • Transfers amount from source_id to target_id.
    • Must satisfy invariants: source_id and target_id both exist, source_id != target_id, amount > 0, and source_id has a balance >= amount.
    • If valid, deduct amount from source_id, add amount to target_id, and increment source_id's total outgoing transfer volume by amount.
    • Returns "true" if successful, or "false" if any validation fails (no state changes on failure).
  3. TOP_ACTIVITY

    • Returns the top k account IDs ranked by cumulative outgoing transfer volume in descending order.
    • In case of a tie in volume, sort lexicographically (alphabetical order) by account_id ascending.
    • If fewer than k accounts exist, return all accounts in ranked order.
    • Format output as a comma-separated list of IDs, e.g. "ACC1,ACC2". If no accounts exist or `k list[str]: """ Executes a sequence of string commands and returns the result of each command. """ pass

---

## Example

### Input
```python
commands = [
    "CREATE A",
    "CREATE B",
    "CREATE C",
    "DEPOSIT A 100",
    "DEPOSIT B 50",
    "TRANSFER A B 30",
    "TRANSFER B C 20",
    "TRANSFER A C 10",
    "TOP_ACTIVITY 2",
    "TRANSFER C A 100",  # C only has 30, should fail
    "TOP_ACTIVITY 3"
]

Output

[
    "true",
    "true",
    "true",
    "100",
    "50",
    "true",
    "true",
    "true",
    "A,B",
    "false",
    "A,B,C"
]

Explanation

  • After transfers: A has transferred 30 + 10 = 40, B has transferred 20, and C has transferred 0.
  • TOP_ACTIVITY 2 returns "A,B".
  • TRANSFER C A 100 fails because C has a balance of 30 (from TRANSFER B C 20 and TRANSFER A C 10), so balance is insufficient.
  • TOP_ACTIVITY 3 returns "A,B,C".

Constraints

  • 1 <= len(commands) <= 20,000
  • Account IDs consist of alphanumeric characters ([a-zA-Z0-9]), length 1 <= len(account_id) <= 32.
  • 1 <= amount <= 1,000,000,000
  • 1 <= k <= 1,000
  • Balances and transfer amounts fit within standard 64-bit signed integers.

Edge Cases

  • Attempting to transfer to oneself (source_id == target_id).
  • Insufficient funds or zero/negative transaction amounts.
  • Operations on non-existent accounts.
  • k larger than the total number of created accounts.

Hints

Hint 1

Maintain a dedicated Account class or dictionary mapping account IDs to an object tracking balance and total outgoing transfer volume.

Hint 2

Validate all preconditions before modifying any balances in the transfer operation to ensure atomicity.

Hint 3

For activity rankings, sort the accounts using a compound key `(-outgoing_volume, account_id)` to handle the descending volume and ascending alphabetical order simultaneously.

Follow-up questions

  • How would you optimize `TOP_ACTIVITY` if calls are frequent and the total number of accounts is very large (e.g., millions)?
  • How would you adapt the system to handle concurrent requests safely across multiple threads?
  • How would you extend this design to support transaction rollbacks or scheduled pending transfers with an expiration window?