Problem · Hash Table

Banking System with Top Activity

Learn this problem
MediumCapital One logoCapital OneFULLTIMEONSITE INTERVIEW

Problem statement

Implement a banking system that processes a sequence of operations. Initially, the system contains no accounts. Each account has a balance and a financial-activity total, both starting at zero.

Each row of operations has one of these forms:

  • ["CREATE_ACCOUNT", accountId]: If the account already exists, return false. Otherwise, create it and return true.
  • ["DEPOSIT", accountId, amount]: If the account does not exist, return -1. Otherwise, add amount to its balance and activity, then return its updated balance.
  • ["TRANSFER", fromAccountId, toAccountId, amount]: Return -1 if either account does not exist, both identifiers name the same account, or the sender has insufficient funds. Otherwise, move amount from the sender to the recipient, add amount to both accounts' activity, and return the sender's updated balance.
  • ["TOP_ACTIVITY", n]: Return up to n account descriptions formatted as accountId(activity). Order them by activity from highest to lowest, breaking ties by accountId in ascending alphabetical order. If fewer than n accounts exist, return all accounts in that order.

An account's financial activity is the absolute sum of amounts from its successful deposits and transfers. A successful transfer contributes its amount to the activity of both endpoints. Unsuccessful transactions do not contribute activity.

Return one string array for every operation, in the same order. A scalar result is represented as a one-element array, such as ["true"] or ["2700"]; a TOP_ACTIVITY result contains its ranked account descriptions directly.

What the interview report shared

The report gave all four operation rules, the activity-ranking rule, and a complete operation/output sequence. This practice version is an estimated 95% match to that core task; the report did not include numeric limits or the original method and parameter names, so those details are not presented as source-provided.

Function

processBankingOperations(operations: String[][]) → String[][]

Examples

Example 1

operations = [["CREATE_ACCOUNT","account1"],["CREATE_ACCOUNT","account1"],["CREATE_ACCOUNT","account2"],["DEPOSIT","non-existing","2700"],["DEPOSIT","account1","2700"],["TRANSFER","account1","account2","2701"],["TRANSFER","account1","account2","200"],["TRANSFER","account1","account2","2500"],["DEPOSIT","account2","300"],["CREATE_ACCOUNT","account3"],["DEPOSIT","account3","4000"],["TOP_ACTIVITY","3"],["DEPOSIT","account2","1000"],["TOP_ACTIVITY","2"],["TOP_ACTIVITY","5"]]return = [["true"],["false"],["true"],["-1"],["2700"],["-1"],["2500"],["0"],["3000"],["true"],["4000"],["account1(5400)","account3(4000)","account2(3000)"],["4000"],["account1(5400)","account2(4000)"],["account1(5400)","account2(4000)","account3(4000)"]]

The first deposit gives account1 a balance and activity of 2700. The transfer of 2701 fails, while the transfers of 200 and 2500 succeed, leaving account1 with balance 0 and activity 5400. Those transfers give account2 activity 2700; after its deposit of 300, its balance and activity are 3000. With account3's deposit of 4000, the first ranking is therefore account1(5400), account3(4000), account2(3000).

After another deposit of 1000 into account2, accounts 2 and 3 both have activity 4000. Their tie is resolved alphabetically, so account2 comes first.

More Capital One problems

drafts saved locally
public String[][] processBankingOperations(String[][] operations) {
    // write your code here
}
operations[["CREATE_ACCOUNT","account1"],["CREATE_ACCOUNT","account1"],["CREATE_ACCOUNT","account2"],["DEPOSIT","non-existing","2700"],["DEPOSIT","account1","2700"],["TRANSFER","account1","account2","2701"],["TRANSFER","account1","account2","200"],["TRANSFER","account1","account2","2500"],["DEPOSIT","account2","300"],["CREATE_ACCOUNT","account3"],["DEPOSIT","account3","4000"],["TOP_ACTIVITY","3"],["DEPOSIT","account2","1000"],["TOP_ACTIVITY","2"],["TOP_ACTIVITY","5"]]
expected[["true", "false", "true", "-1", "2700", "-1", "2500", "0", "3000", "true", "4000", "account1(5400)", "account3(4000)", "account2(3000)", "4000", "account1(5400)", "account2(4000)", "account1(5400)", "account2(4000)", "account3(4000)"]]
checking account