Simple Bank System
On this page
Problem statement
Build a small bank with accounts numbered 1 to n and their starting balances. Every request carries a time
tthat never decreases.
Level 1. deposit(t, a, amount), withdraw(t, a, amount) and transfer(t, a, b, amount) return True if the request is valid and applied, False otherwise. A request is invalid if an account doesn't exist, the payer doesn't have enough money, or a transfer goes from an account to itself. An invalid request changes nothing. Level 2. top_spenders(k) returns the k accounts that have sent the most money (withdrawals plus outgoing transfers), highest first, ties broken by the smaller account number. Level 3. balance_at(a, t) returns what account a held right after all requests up to time t.
Level 1 is LeetCode 2043, Simple Bank System. In interviews it's usually the first stage of a longer, multi-level exercise where each stage adds a query on top of the same accounts, so it pays to design for the later levels from the start.
Example

Bank([50, 120, 0])
withdraw(1, #1, 30) -> True # #1: 20
transfer(2, #2, #3, 70) -> True # #2: 50, #3: 70
transfer(3, #1, #3, 40) -> False # #1 has only 20
deposit(4, #4, 10) -> False # no account #4
transfer(5, #3, #1, 5) -> True # #3: 65, #1: 25
top_spenders(2) -> [#2, #1] # sent 70 and 30
balance_at(#3, 4) -> 70
balance_at(#3, 5) -> 65
Constraints (assumed)
- 1 ≤ n ≤ 10⁵ accounts; up to 10⁵ requests.
- Amounts and balances fit in a 64-bit integer.
- Times are integers and never decrease; several requests may share a time.
What this question tests
- Validating before mutating, so a failed request leaves no trace.
- Off-by-one care with 1-based account numbers.
- Keeping small side structures up to date so that later queries are cheap.
Questions to ask first
- Are account numbers 1-based? (Here: yes, 1 to n.)
- Is a transfer to the same account allowed? (Here: no, it's rejected.)
- Does a deposit count as spending? (Here: no; only withdrawals and outgoing transfers.)
- How are ties ordered in
top_spenders? (Here: smaller account number first.) - Can amounts be negative or zero? (Here: they're non-negative.)
Try it yourself first
You have the full problem. Sketch your answer, then sign up free to read the complete walkthrough.
- The complete design, flow by flow, with pictures
- Deep dives into the hard parts
- What mid, senior and staff candidates are expected to say
- Full company question lists
Free forever. No card needed.