Machine Coding an Idempotent In-Memory Wallet Service
Last updated: November 29, 2026
Quick Overview
In 90 minutes, build an in-memory wallet service that supports creating wallets, adding money, transferring between users and fetching a recent statement, where retried requests with the same idempotency key never move money twice.
PhonePe
November 29, 20260
0
0 solved
In 90 minutes, build an in-memory wallet service that supports creating wallets, adding money, transferring between users and fetching a recent statement, where retried requests with the same idempotency key never move money twice.
PhonePe SWE loops typically include a machine coding round where working, well-structured code matters more than a perfect algorithm, and a wallet is close to the company's real money-movement problems. The interviewer checks class design, integer money, thread safety without deadlocks, and idempotency semantics on retries.
What the Interviewer Expects
- Represent money as integer paise and validate amounts
- Make every money operation idempotent on a client-supplied key
- Lock both wallets for a transfer in a fixed order to avoid deadlock
- Keep an append-only ledger per user that backs the statement
- Separate failure semantics: a failed attempt should be retryable
- Leave the code demoable with a small driver
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- What should a second caller see if it arrives while the first request with the same key is still running?
- How would this design change when wallets live in a sharded MySQL database instead of memory?
- How would you add a daily debit limit per wallet?
- How do you prove the system conserves money?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
Operations: create(user), add_money(user, paise, key), transfer(src, dst, paise, key), balance(user), statement(user, limit). A repeated key returns the original result without side effects....
High-Level Design
A WalletService owns balances, a lock per wallet, a per-user ledger of entries (txn id, signed delta, resulting balance, note), and an idempotency map. A small _once helper reserves the key, runs ...