Anthropic CodingFree previewFree
Bank Transfers With Merge
Last asked
Problem
Implement a bank across four CodeSignal levels. Start with deposits, withdrawals, and transfers, then extend the bank to hold funds for two-step transfers and merge accounts without confusing old and recreated accounts.
Starter code
class BankingSystem:
def create_account(self, timestamp: int, account_id: str) -> bool:
raise NotImplementedError
def deposit(self, timestamp: int, account_id: str, amount: int) -> int | None:
raise NotImplementedError
def withdraw(self, timestamp: int, account_id: str, amount: int) -> int | None:
raise NotImplementedError
def transfer(
self, timestamp: int, source_id: str, target_id: str, amount: int
) -> int | str | None:
# Levels 1–2 transfer funds immediately. Levels 3–4 return a pending transfer ID.
raise NotImplementedError
def transfer_accept(self, timestamp: int, account_id: str, transfer_id: str) -> bool:
raise NotImplementedError
def merge_account(self, timestamp: int, source_id: str, target_id: str) -> bool:
raise NotImplementedErrorLevels 1–2
Implement create_account, deposit, withdraw, and transfer. In these levels, transfer moves funds between two accounts atomically.
Level 3 — Two-step transfer
Extend transfer to put funds on hold and return a transfer ID. Until the recipient calls transfer_accept, the sender's available balance is reduced and the recipient's balance is unchanged. The expiry policy for unaccepted transfers varies across reports.
Level 4 — Merge accounts
Use merge_account to fold the source account into the target account, preserving pending transfers and history. If the source ID is created again with create_account, the new account must start with its own history. One approach is to key ledger entries by (account_id, generation).
Variant — Cashback and top spenders
A separate four-level Bank System OA (the alternate canonical variant) replaces transfer_accept with payment and cashback operations. Its reported signatures are:
class BankingSystem:
# Level 1 — accounts + atomic transfers
def create_account(self, timestamp: int, account_id: str) -> bool:
# True on creation; False if account_id is already taken.
raise NotImplementedError
def deposit(self, timestamp: int, account_id: str, amount: int) -> int | None:
# Returns new balance, or None if the account is missing.
raise NotImplementedError
def transfer(self, timestamp: int, source_id: str, target_id: str, amount: int) -> int | None:
# Returns the source's new balance.
# None if: either account missing, source == target, or insufficient funds.
raise NotImplementedError
# Level 2 — ranking by outgoing volume
def top_spenders(self, timestamp: int, n: int) -> list[str]:
# Returns ["id1(spent_amount)", "id2(spent_amount)"]
# Sort: outgoing-total desc, then account_id alphabetical asc.
# Outgoing counts transfer + pay (Level 3), NOT cashback inflows.
# If fewer than n accounts exist, return all of them.
raise NotImplementedError
# Level 3 — payments with cashback
def pay(self, timestamp: int, account_id: str, amount: int) -> str | None:
# Returns a unique payment id (e.g. "payment1"), or None if the account
# is missing or has insufficient funds.
# Cashback = floor(amount * 0.02) added back to the account exactly
# 86,400,000 ms (= 24 h) after `timestamp`.
# The withdrawal counts toward `top_spenders` outgoing totals.
raise NotImplementedError
def get_payment_status(self, timestamp: int, account_id: str, payment: str) -> str | None:
# Returns "IN_PROGRESS" or "CASHBACK_RECEIVED".
# None if the account or payment id is missing, OR if the payment id
# belongs to a different account.
raise NotImplementedError
# Level 4 — merge + look-back history
def merge_accounts(self, timestamp: int, account_id_1: str, account_id_2: str) -> bool:
# Merges account_2 INTO account_1. account_2 is deleted.
# account_1 inherits: balance, outgoing-spend history, pending cashbacks,
# and the ability to look up account_2's old payment statuses via its own id.
# False if account_1 == account_2 or either is missing.
raise NotImplementedError
def get_balance(self, timestamp: int, account_id: str, time_at: int) -> int | None:
# Balance of account_id at the exact moment time_at.
# None if the account did not exist at time_at.
# After a merge: account_1 inherits account_2's pre-merge balance history;
# a *new* account_2 created post-merge starts at $0 with no inherited history.
raise NotImplementedErrorpayschedules cashback equal to 2% of the payment, rounded down, exactly 86,400,000 ms later. Timestamps are strictly increasing.top_spendersranks accounts by cumulative outgoing transfers and payments, breaking ties by ascending account ID.merge_accountscombines the two accounts' outgoing totals.- After a merge, use
get_balanceto check whether an account existed at the requested time and keep the history of a recreated ID separate. - Use the signatures your harness provides. The
payandtop_spendersvariant and thetransfer_acceptvariant have different APIs.
Loading the solution…
