Back to Practice
#0072
Bank Account Class
EasyPython18 min15 XP
Problem
Implement BankAccount with balance, deposit(amount), and withdraw(amount). Deposit and withdraw should reject negative amounts with ValueError. withdraw should return False if funds are insufficient; otherwise update balance and return True.
Why This Matters
OOP interviews often test whether you understand instance state, validation, and method behavior more than inheritance trivia.
Function Signature
class BankAccount:
Examples
Example 1
Inputacct = BankAccount(100); acct.deposit(25); acct.withdraw(40)
OutputTrue and balance 85
The instance keeps its own balance as methods change state.
Constraints
- Use instance attribute self.balance.
- Reject negative amounts.
- Do not allow overdraft.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Deposit and withdraw
Inputstart 100, deposit 25, withdraw 40
Expectedbalance 85
Both methods update the same account instance.
Insufficient funds
Inputstart 10, withdraw 50
ExpectedFalse and balance 10
Failed withdrawal should not change balance.
Hidden Test Categories
default balancenegative depositnegative withdrawal