Problem · Design

Banking Transaction Exceptions

Learn this problem
MediumVirtu Financial logoVirtu FinancialFULLTIMEOA

Problem statement

Implement the behavior of a bank account that starts with a balance of 1500.0. Process the rows of operations from left to right while maintaining the current balance and a history of successful transactions.

Transaction records

Each successful deposit or withdrawal creates a transaction record containing its type, amount, and resulting balance. Its string representation is:

Type: {transaction_type}, Amount: {amount}, Balance: {balance}

Amounts are positive whole numbers. Balances are displayed with a trailing .0.

Operations

Each row has one of these forms:

  • ["deposit", amount]: Add amount to the balance and append a deposit record. Return ["Transaction successful!", "Updated account balance: {balance}"].
  • ["withdraw", amount]: If the balance is sufficient, subtract amount, append a withdrawal record, and return the same two success messages. Otherwise return ["InsufficientFundsError"], leave the balance unchanged, and do not modify history.
  • ["view_transaction_history"]: Return every successful transaction record in chronological order. Return an empty row when there is no history.

Return one string row for every operation, in the same order.

Function

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

Examples

Example 1

operations = [["deposit","200"],["withdraw","500"],["view_transaction_history"]]return = [["Transaction successful!","Updated account balance: 1700.0"],["Transaction successful!","Updated account balance: 1200.0"],["Type: deposit, Amount: 200, Balance: 1700.0","Type: withdraw, Amount: 500, Balance: 1200.0"]]

The deposit raises the balance from 1500.0 to 1700.0. The withdrawal then lowers it to 1200.0. The final operation returns both successful transaction records in order.

Example 2

operations = [["withdraw","1600"],["view_transaction_history"]]return = [["InsufficientFundsError"],[]]

The account has only 1500.0, so the withdrawal fails without changing the balance or history. Viewing history therefore returns an empty row.

Example 3

operations = [["view_transaction_history"],["deposit","50"],["view_transaction_history"],["withdraw","1550"],["view_transaction_history"]]return = [[],["Transaction successful!","Updated account balance: 1550.0"],["Type: deposit, Amount: 50, Balance: 1550.0"],["Transaction successful!","Updated account balance: 0.0"],["Type: deposit, Amount: 50, Balance: 1550.0","Type: withdraw, Amount: 1550, Balance: 0.0"]]

The first history view is empty. After the deposit, history contains one record. Withdrawing exactly the full balance succeeds, leaves 0.0, and adds the second record.

Constraints

  • 1 <= operations.length <= 2000
  • Each operation is exactly one of deposit, withdraw, or view_transaction_history.
  • Deposit and withdrawal rows contain a base-10 positive integer amount string in the range 1 through 10^9.
  • The running balance always fits in a signed 64-bit integer.
  • A history view does not change the account state.

More Virtu Financial problems

drafts saved locally
public String[][] simulateBankingTransactions(String[][] operations) {
    // write your code here
}
operations[["deposit","200"],["withdraw","500"],["view_transaction_history"]]
expected[["Transaction successful!", "Updated account balance: 1700.0", "Transaction successful!", "Updated account balance: 1200.0", "Type: deposit", "Amount: 200", "Balance: 1700.0", "Type: withdraw", "Amount: 500", "Balance: 1200.0"]]
checking account