Design a Bank Account Class

Quick Overview

Design a BankAccount class for one holder with deposits, withdrawals, transfers to another account and balance lookups, where invalid or unaffordable amounts return False. It tests input validation, encapsulation, and making a transfer change both accounts or neither.

Design a Bank Account Class

Company: Mastercard

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: easy

Interview Round: Other

Design a `BankAccount` class that models a simple bank account for a single account holder. The account is initialized with an account holder name and a starting balance. It supports deposits, withdrawals, transfers to another `BankAccount`, and balance inquiries. Withdrawals and transfers must fail (return `False`) when the account has insufficient funds or the amount is non-positive. Deposits with non-positive amounts must also fail. Successful operations return `True`. Implement the following: - `BankAccount(name: str, initial_balance: int)`: create the account with the holder's name and starting balance. - `deposit(amount: int) -> bool`: add `amount` to the balance. Returns `True` on success, or `False` if `amount <= 0`. - `withdraw(amount: int) -> bool`: subtract `amount` from the balance. Returns `True` on success, or `False` if `amount <= 0` or `amount > balance`. - `transfer(other: BankAccount, amount: int) -> bool`: transfer `amount` from this account to `other`. Returns `True` on success, or `False` if the withdrawal would fail. - `get_balance() -> int`: return the current balance of the account. - `get_name() -> str`: return the account holder's name. Example 1: ```text Input: ["BankAccount", "deposit", "withdraw", "get_balance", "withdraw", "get_balance", "get_name"] [["Alice", 100], [50], [30], [], [200], [], []] Output: [null, true, true, 120, false, 120, "Alice"] Explanation: acct = BankAccount("Alice", 100) # name = "Alice", balance = 100 acct.deposit(50) # → True balance: 150 acct.withdraw(30) # → True balance: 120 acct.get_balance() # → 120 acct.withdraw(200) # → False insufficient funds, balance: 120 acct.get_balance() # → 120 acct.get_name() # → "Alice" ``` Example 2: ```text Input: ["BankAccount", "deposit", "deposit", "withdraw", "get_balance"] [["Bob", 0], [0], [100], [-10], []] Output: [null, false, true, false, 100] Explanation: acct = BankAccount("Bob", 0) # name = "Bob", balance = 0 acct.deposit(0) # → False non-positive deposit acct.deposit(100) # → True balance: 100 acct.withdraw(-10) # → False non-positive withdrawal acct.get_balance() # → 100 ``` Example 3 (two accounts): ```text alice = BankAccount("Alice", 100) bob = BankAccount("Bob", 20) alice.transfer(bob, 70) # → True alice: 30, bob: 90 alice.transfer(bob, 50) # → False alice has only 30, nothing changes bob.transfer(alice, -5) # → False non-positive amount alice.get_balance() # → 30 bob.get_balance() # → 90 ``` ```hint Order of checks List everything that can make a call fail and check all of it before you change a balance. For `transfer`, ask which method you already wrote knows those rules, and what could still go wrong after it succeeds. ``` ### Constraints & Assumptions - `1 <= len(name) <= 100` - `0 <= initial_balance <= 10⁹` - All amounts are integers in `[-10⁹, 10⁹]`. - At most `10⁴` operations will be performed. ### Clarifying Questions to Ask - Should a transfer from an account to itself be allowed, and should it report success? - Can `other` be `None` or something that is not a `BankAccount`, and should that return `False` or raise? - Will several threads call methods on the same account at the same time? - Should failures stay as `True`/`False` return values, or does the caller need to know why a call failed? ### What a Strong Answer Covers - Validation for each method that changes money, done before any balance changes, so a failed call leaves both accounts as they were. - A `transfer` that applies the withdrawal rules from one place and cannot take money out of one account without adding it to the other. - Encapsulated state: the name and balance are read through methods and cannot be set from outside. - Boundary cases such as a zero or negative amount, withdrawing the exact balance, and a transfer to the same account. - Constant time per operation, and a balance type that cannot overflow at the stated limits. ### Follow-up Questions - Two threads run `a.transfer(b, x)` and `b.transfer(a, y)` at the same moment. How do you keep both balances correct without deadlocking? - How would you add a transaction history that lets you audit or rebuild the balance? - How would you support an overdraft limit, or a savings account with different withdrawal rules, without copying the class? - When would you raise exceptions in place of returning `False`, and what does that change for callers?

Overview: Design a BankAccount class for one holder with deposits, withdrawals, transfers to another account and balance lookups, where invalid or unaffordable amounts return False. It tests input validation, encapsulation, and making a transfer change both accounts or neither.

Mastercard logo
Mastercard
Nov 25, 2026
easySoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a BankAccount class that models a simple bank account for a single account holder.

The account is initialized with an account holder name and a starting balance. It supports deposits, withdrawals, transfers to another BankAccount, and balance inquiries.

Withdrawals and transfers must fail (return False) when the account has insufficient funds or the amount is non-positive. Deposits with non-positive amounts must also fail. Successful operations return True.

Implement the following:

  • BankAccount(name: str, initial_balance: int) : create the account with the holder's name and starting balance.
  • deposit(amount: int) -> bool : add amount to the balance. Returns True on success, or False if amount <= 0 .
  • withdraw(amount: int) -> bool : subtract amount from the balance. Returns True on success, or False if amount <= 0 or amount > balance .
  • transfer(other: BankAccount, amount: int) -> bool : transfer amount from this account to other . Returns True on success, or False if the withdrawal would fail.
  • get_balance() -> int : return the current balance of the account.
  • get_name() -> str : return the account holder's name.

Example 1:

Input:
["BankAccount",  "deposit", "withdraw", "get_balance", "withdraw", "get_balance", "get_name"]
[["Alice", 100], [50],      [30],       [],            [200],      [],            []]

Output: [null, true, true, 120, false, 120, "Alice"]

Explanation:
acct = BankAccount("Alice", 100)   # name = "Alice", balance = 100
acct.deposit(50)                   # → True   balance: 150
acct.withdraw(30)                  # → True   balance: 120
acct.get_balance()                 # → 120
acct.withdraw(200)                 # → False  insufficient funds, balance: 120
acct.get_balance()                 # → 120
acct.get_name()                    # → "Alice"

Example 2:

Input:
["BankAccount", "deposit", "deposit", "withdraw", "get_balance"]
[["Bob", 0],    [0],       [100],     [-10],      []]

Output: [null, false, true, false, 100]

Explanation:
acct = BankAccount("Bob", 0)       # name = "Bob", balance = 0
acct.deposit(0)                    # → False  non-positive deposit
acct.deposit(100)                  # → True   balance: 100
acct.withdraw(-10)                 # → False  non-positive withdrawal
acct.get_balance()                 # → 100

Example 3 (two accounts):

alice = BankAccount("Alice", 100)
bob = BankAccount("Bob", 20)

alice.transfer(bob, 70)            # → True   alice: 30, bob: 90
alice.transfer(bob, 50)            # → False  alice has only 30, nothing changes
bob.transfer(alice, -5)            # → False  non-positive amount
alice.get_balance()                # → 30
bob.get_balance()                  # → 90

Constraints & Assumptions

  • 1 <= len(name) <= 100
  • 0 <= initial_balance <= 10⁹
  • All amounts are integers in [-10⁹, 10⁹] .
  • At most 10⁴ operations will be performed.

Clarifying Questions to Ask Guidance

  • Should a transfer from an account to itself be allowed, and should it report success?
  • Can other be None or something that is not a BankAccount , and should that return False or raise?
  • Will several threads call methods on the same account at the same time?
  • Should failures stay as True / False return values, or does the caller need to know why a call failed?

What a Strong Answer Covers Guidance

  • Validation for each method that changes money, done before any balance changes, so a failed call leaves both accounts as they were.
  • A transfer that applies the withdrawal rules from one place and cannot take money out of one account without adding it to the other.
  • Encapsulated state: the name and balance are read through methods and cannot be set from outside.
  • Boundary cases such as a zero or negative amount, withdrawing the exact balance, and a transfer to the same account.
  • Constant time per operation, and a balance type that cannot overflow at the stated limits.

Follow-up Questions Guidance

  • Two threads run a.transfer(b, x) and b.transfer(a, y) at the same moment. How do you keep both balances correct without deadlocking?
  • How would you add a transaction history that lets you audit or rebuild the balance?
  • How would you support an overdraft limit, or a savings account with different withdrawal rules, without copying the class?
  • When would you raise exceptions in place of returning False , and what does that change for callers?