Stage 1 of 3: Accounts, transfers, ranking
Background. A bank holds accounts, each under a string id with an integer balance. Money is added to accounts and moved between them, and the bank ranks its accounts by how much money each has sent. Every call is told the time at which it is made.
Implement the class Bank in bank.py, with the methods open_account, add_funds, move_funds, balance and top_outflow.
open_account(now, account_id)opens an account underaccount_idwith balance 0 and returnsTrue. If the bank already holds an account with this id, it returnsFalse.add_funds(now, account_id, amount)addsamountto the account's balance and returns the new balance.move_funds(now, from_id, to_id, amount)movesamountfrom the accountfrom_idto the accountto_idand returns the sender's new balance. It is refused whenfrom_idandto_idare the same id or when the sender's balance is belowamount; sending exactly the whole balance is allowed.balance(now, account_id)returns the account's balance.top_outflow(now, n)ranks the accounts by money sent: the total of the amounts each has sent with successfulmove_fundscalls. It returns the firstnas(account_id, money_sent)tuples, the largest first, ties broken by account id in ascending Python string order ("Zed"before"amy","a10"before"a9"). Every account the bank holds is ranked, one that has sent nothing with 0; with fewer thannaccounts, all of them are returned.
add_funds, move_funds and balance return None when an id they are given names no account. A refused call, one that returns False or None, changes nothing.
now is the time of the call in milliseconds: a positive integer up to 10^15, larger on every call than on the one before. No answer here depends on it.
Example
b = Bank()
b.open_account(1, "ines") # True
b.open_account(2, "tomas") # True
b.open_account(3, "ines") # False: the bank already holds "ines"
b.add_funds(4, "ines", 700) # 700
b.move_funds(5, "ines", "tomas", 260) # 440: what "ines" has left
b.move_funds(6, "tomas", "ines", 300) # None: "tomas" holds only 260
b.balance(7, "tomas") # 260
b.top_outflow(8, 5) # [("ines", 260), ("tomas", 0)]
Notes
- Account ids are non-empty strings of any characters, and case-sensitive. Amounts are positive integers of any size, and
nis at least 1. Inputs are always valid. - All 14 tests in
tests/test_part1.pyfail on the starter.
Done when Submit goes green.
Constraints
- Python 3.12 standard library only
What comes after stage 1
The next stages open once the tests for the one before them pass. Each raises the bar, the way an interviewer does in the room.
- Stage 2: Cashback a day later. Card payments pay a little back a day later, and every call has to agree on the moment it lands.
- Stage 3: Merged accounts and past balances. Accounts fold into one another, and the bank is asked what a balance was at an earlier moment.
The files you get
- Yours to edit: bank.py.
- Read-only: tests/__init__.py.
- Tests you can read and run: tests/test_part1.py. Hidden tests run on Submit.
The round: no AI, an assistant that only answers
Anthropic runs this round without AI. Here the closest format is answer-only: the assistant answers your questions and reviews your code when you ask, the way an interviewer would, and never writes a line of it. The interviewer asks about your decisions as you go, and the report states the format. You can switch to the agent format when you press Begin.
Frequently asked, briefly answered
Is Banking System free?
It is a Pro problem. Free plan includes one complete problem, plus the first stage of several more, so you can see how a round works before you pay.
Which company asks something like this?
Anthropic, Coinbase, Meta. It is modelled on Anthropic's coding round, which runs without AI. Based on candidates' reports of Anthropic interviews. Nyrion wrote this version, so the question you get may differ.
Do I bring my own AI tool?
No. The real round has no AI, so this one opens answer-only: an assistant that answers your questions and can't write code. You can switch to an agent that proposes changes when you press Begin.
What does the interviewer ask?
6 questions along the way about your own code, and one at the end about the whole session. A weak answer gets one follow up. The answers go into the report, which ends with a hire or no hire verdict.
Start the problem
Sign in and the problem opens where you left it. New here? An account takes a minute to create, and the Pro plan starts at the first stage of this problem.
More like it: every practice problem, Anthropic's interviews, the AI coding interview guide.