Preparing your design workspace

Loading the problem and your saved attempt…

Design and Implement a Concurrency-Safe Wallet

Model an in-memory wallet whose credits and debits preserve a nonnegative balance when requests race.

For this exercise, assume an application needs an in-memory wallet component. A client can open a wallet with a nonnegative initial balance, query its balance, credit a positive amount, and debit a positive amount. Amounts use integer cents.

A debit succeeds only when sufficient funds exist, and the balance must never become negative. Each successful credit or debit returns the resulting balance; an unknown wallet, non-positive amount, or insufficient funds produces an explicit typed result without changing state.

Multiple threads may credit or debit the same wallet concurrently, so checking funds and applying a debit must be one atomic transition. The component runs in one process. Transfers, persistence, currency conversion, authentication, external payment calls, and distributed coordination are out of scope.

Design the object model, contracts, and interactions, then implement the bounded in-memory debit operation.