Compute Available Balances From a Ledger With Holds
Last updated: November 29, 2026
Quick Overview
Replay a stream of deposit, hold, release and settle events for one account and report, per asset, the total balance, the amount on hold, and the amount available to trade or withdraw. A hold that exceeds the available balance must be rejected.
Gemini
November 29, 20260
0
0 solved
Replay a stream of deposit, hold, release and settle events for one account and report, per asset, the total balance, the amount on hold, and the amount available to trade or withdraw. A hold that exceeds the available balance must be rejected.
Gemini places holds on funds when a user opens an order or requests a withdrawal, and the difference between total and available balance is something every exchange engineer must get exactly right. The interviewer wants clean state modeling, exact decimal math and a correct insufficient-funds check.
What the Interviewer Expects
- Model holds separately from the total instead of decrementing the balance on hold
- Reject a hold larger than available funds, not larger than total funds
- Use Decimal for amounts and keep results per asset
- Treat settle as converting a hold into a real debit
- Discuss idempotency for duplicate or unknown event ids
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you make replay idempotent if the same event can be delivered twice?
- How would you support partial settlement when an order is partly filled?
- How does this map onto double-entry bookkeeping with a holds account?
- What changes if thousands of holds per asset make the sum too slow?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Events arrive in order. deposit adds to total. hold reserves an amount under an id (an open order or a pending withdrawal) and must fail if amount > total - held. release cancels a hold (order...
High-Level Design
Keep total[asset] and a map holds[id] = (asset, amount). Never subtract on hold; available is derived. This mirrors how an exchange avoids double spending: two orders cannot both reserve the same ...