Design Digital Wallet (Low-Level Design)
Deposits, withdrawals and transfers that are safe to retry, with a full history. Focus: Idempotent money operations and a ledger.
| Difficulty | MEDIUM |
|---|---|
| Design focus | Idempotent money operations and a ledger |
| Automated grading | 3 public + 8 hidden tests (11 total) |
| Phase 2 follow-up | Fee plans |
| Language | TypeScript (OOP / Machine Coding) |
Problem statement
Amounts are whole cents. Every operation carries a requestId chosen by the caller.
Retrying with a requestId that already succeeded must not move money again: it returns the original result (the balance after the first deposit or withdrawal).
A failed operation (unknown account, not enough money, bad amount) changes nothing and does not use up its requestId.
Functional requirements
- createAccount(id) (an existing id throws); balance(id).
- deposit(id, amount, requestId) and withdraw(id, amount, requestId) return the new balance.
- transfer(from, to, amount, requestId) moves money; transferring to yourself throws.
- history(id) lists entries { type, amount, requestId } in order, with types DEPOSIT, WITHDRAW, TRANSFER_IN and TRANSFER_OUT.
Required API & starter interface
Implement the TypeScript class interface below. You can add any helper classes, policies or strategies you need:
type EntryType = "DEPOSIT" | "WITHDRAW" | "TRANSFER_IN" | "TRANSFER_OUT";
class Wallet {
createAccount(id: string): void {
throw new Error("Not implemented");
}
deposit(id: string, amount: number, requestId: string): number {
throw new Error("Not implemented");
}
withdraw(id: string, amount: number, requestId: string): number {
throw new Error("Not implemented");
}
transfer(from: string, to: string, amount: number, requestId: string): void {
throw new Error("Not implemented");
}
balance(id: string): number {
throw new Error("Not implemented");
}
history(id: string): { type: EntryType; amount: number; requestId: string }[] {
throw new Error("Not implemented");
}
}
Public test cases
Your code runs against 3 public tests plus 8 hidden edge-case tests in a sandbox:
Deposit and withdraw
var w = new Wallet();
w.createAccount("a");
assertEqual(w.deposit("a", 500, "r1"), 500);
assertEqual(w.withdraw("a", 200, "r2"), 300);
Retries do not double-charge
var w = new Wallet();
w.createAccount("a");
w.deposit("a", 500, "r1");
assertEqual(w.withdraw("a", 100, "r2"), 400);
assertEqual(w.withdraw("a", 100, "r2"), 400);
assertEqual(w.balance("a"), 400);
Not enough money
var w = new Wallet();
w.createAccount("a");
assertThrows(function () { w.withdraw("a", 1, "r1"); });
Design & structure checks
Beyond passing tests, the grader inspects your class structure for clean object-oriented design:
- Fee plans are separate policies: New plans (a cap, a free monthly allowance) should be new classes, not new branches in transfer().
- Every change is a ledger entry with a named type: A history of typed entries is how real wallets explain and audit every balance.
Phase 2: Change request
In real interviews, the follow-up tests whether your design is extensible. After your first version passes all 11 tests, you unlock the change request: Fee plans.
Pair with High-Level System Design
Ready to scale Digital Wallet across distributed servers? Try the matching HLD simulator problem: Design Digital Wallet & Payment Ledger.
Relevant low-level design lessons
Safe retries with request ids
Make a money operation safe to call twice.
Ordering with ties
Write one comparison rule that every part of the code agrees on.
Strategy: swap behaviour, not code
Replace an if/else on a type with interchangeable strategy classes.
More low-level design problems
Design Expense Sharing (Splitwise)
Split shared expenses several ways, track who owes whom, and suggest the fewest payments to settle up.
Design Movie Ticket Booking
Hold seats for a few minutes, confirm or release them, and never sell one seat twice.
Design LRU Cache
A fixed-size cache that throws out the least recently used entry when full, with every operation in constant time.
Design Rate Limiter
Two rate-limiting algorithms behind one interface, so callers can swap them.