Katkat

Implement deposit, withdraw, and transfer in a class

Capital OneSoftware Engineer · Onsite

Last 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

  • amount is a positive integer.
  • withdraw must fail (return false or throw) if the account has insufficient funds.
  • transfer must 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.