SDE Path

Digital Wallet

Medium

Digital Wallet

A minimal money-movement core: provision accounts, credit and debit them, and transfer between two accounts atomically.

Requirements

  • Accounts are provisioned explicitly with createAccount (initial balance 0); a duplicate createAccount is ignored and never resets an existing balance.
  • credit, debit, and transfer act only on accounts that already exist. Crediting or debiting an unknown user has no effect / fails, and balance of an unknown user is 0.
  • debit succeeds only when the balance is at least amt, then subtracts it.
  • transfer succeeds only when both accounts exist and the sender holds at least amt; it then moves the funds atomically — a rejected transfer leaves both balances exactly as they were (no partial debit).
  • All amounts are positive integers — think of the smallest currency unit (paise/cents), so there is no rounding.

API to implement

Implement the class Wallet. The I/O driver is already written in the starter code — it parses the command script below and calls your methods. You only implement the class.

| Command | Returns | Behavior | |---|---|---| | createAccount <user> | — | Open an account for user with balance 0. A duplicate call for an existing user is ignored (it never resets the balance). | | credit <user> <amt> | — | Add amt to user's balance. No-op if the account doesn't exist. | | debit <user> <amt> | boolean | Subtract amt from user's balance. Returns true only if the account exists and holds at least amt (then subtracts); otherwise false. | | transfer <fromUser> <toUser> <amt> | boolean | Move amt from fromUser to toUser. Returns true only if both accounts exist and fromUser holds at least amt — then moves the funds atomically. Otherwise false and nothing changes. | | balance <user> | int | Current balance of user. Returns 0 for an unknown user. |

Input format

The first line contains Q, the number of operations. Each of the next Q lines is one command as listed above. String arguments contain no spaces.

Output format

For each command with a non-void return, print its result on its own line, in order. The driver does this for you — your methods just return values.

This is a design problem: the hidden tests exercise edge cases and interaction sequences, not performance tricks. Model the state cleanly and the tests will pass.

Sample cases

Example 1
Input
5
createAccount alice
credit alice 100
balance alice
debit alice 40
balance alice
Expected output
100
true
60
Example 2
Input
8
createAccount a
createAccount b
credit a 100
transfer a b 30
balance a
balance b
transfer a b 200
balance a
Expected output
true
70
30
false
70