Implement deposit, withdraw, and transfer in a class
Capital OneSoftware Engineer · OnsiteLast updated
This question evaluates a candidate's competence in concurrent programming, atomic state updates, and class-level account management for basic monetary operations.
Problem
Implement a class AccountService that supports basic money operations on accounts.
Operations
deposit(accountId, amount)withdraw(accountId, amount)transfer(fromAccountId, toAccountId, amount)getBalance(accountId)
Rules
amountis a positive integer.withdrawmust fail (returnfalseor throw) if the account has insufficient funds.transfermust be atomic: either both balances change, or neither changes.- If an account does not exist, treat its initial balance as 0 (or explicitly create on first use).
Concurrency (if discussed)
Assume these methods may be called concurrently from multiple threads. Make the implementation thread-safe and avoid deadlocks.
Input/Output
This is an in-memory class design question; you do not need to build a database or persistence.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.