Simple Bank System

Free with account Updated Nov 19, 2026 · 9 min read
On this page

    Problem statement

    Build a small bank with accounts numbered 1 to n and their starting balances. Every request carries a time t that 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

    Accounts #1, #2, #3 start with $50, $120 and $0. Two of the five requests are rejected and leave every balance as it was.
    Accounts #1, #2, #3 start with $50, $120 and $0. Two of the five requests are rejected and leave every balance as it was.
    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

    1. Are account numbers 1-based? (Here: yes, 1 to n.)
    2. Is a transfer to the same account allowed? (Here: no, it's rejected.)
    3. Does a deposit count as spending? (Here: no; only withdrawals and outgoing transfers.)
    4. How are ties ordered in top_spenders? (Here: smaller account number first.)
    5. Can amounts be negative or zero? (Here: they're non-negative.)