Advertisement

Simple Bank System - LeetCode 2043 Solution

Simple Bank System - Complete Solution Guide

Simple Bank System is LeetCode problem 2043, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You have been tasked with writing a program for a popular bank that will automate all its incoming transactions (transfer, deposit, and withdraw). The bank has n accounts numbered from 1 to n . The initial balance of each account is stored in a 0-indexed integer array balance , with the (i + 1) th account having an initial balance of balance[i] . Execute all the valid transactions. A transaction is valid if: The given account number(s) are between 1 and n , and The amount of money withdrawn or t

Detailed Explanation

The problem simulates a simplified banking system. You're given an initial balance for each account, and you need to implement methods to transfer, deposit, and withdraw money. The key is to ensure transactions are valid: account numbers must be within the valid range (1 to n, where n is the number of accounts), and withdrawals/transfers must not exceed the account's balance. The Bank class should be initialized with an array representing initial balances. The methods `transfer`, `deposit`, and `withdraw` should return `true` if the transaction is successful and `false` otherwise.

Solution Approach

The solution uses a simple array to store account balances. Each operation (transfer, deposit, withdraw) involves checking for valid account numbers and sufficient balance. Array indices are adjusted to account for 0-based indexing while account numbers are 1-based.

Step-by-Step Algorithm

  1. Step 1: Initialize the Bank object with the initial balances, storing them in an array.
  2. Step 2: In the transfer method, first check if account1 and account2 are valid account numbers (between 1 and n, inclusive).
  3. Step 3: If account numbers are valid, check if account1 has sufficient balance to transfer 'money'.
  4. Step 4: If both conditions are met, subtract 'money' from account1's balance and add 'money' to account2's balance. Return true.
  5. Step 5: In the deposit method, check if the account number is valid (between 1 and n, inclusive).
  6. Step 6: If the account number is valid, add 'money' to the account's balance. Return true.
  7. Step 7: In the withdraw method, check if the account number is valid (between 1 and n, inclusive).
  8. Step 8: If the account number is valid, check if the account has sufficient balance to withdraw 'money'.
  9. Step 9: If both conditions are met, subtract 'money' from the account's balance. Return true.
  10. Step 10: If any check fails (invalid account or insufficient balance), return false.

Key Insights

  • Insight 1: Direct array access is efficient for accessing account balances, given account numbers are sequential (1 to n).
  • Insight 2: Input validation is crucial to ensure accounts exist and balances are sufficient before processing transactions.
  • Insight 3: Using long data types is essential to handle large balances and prevent integer overflow issues during calculations.

Complexity Analysis

Time Complexity: O(1)

Space Complexity: O(n)

Topics

This problem involves: Array, Hash Table, Design, Simulation.

Companies

Asked at: Airbnb, Capital One, Circle, Coinbase, Dropbox, PhonePe.