Credit Accounts with Scheduled Transfers
You need to change top_spenders() in level 2 when you reach level 3 with schedule_payment().
get_balance(time_at) in level 4 needs snapshots, so you need to create snapshots in levels 1, 2, and 3.
The provided tests were all small samples, mainly focused on functionality rather than performance. So I only used dictionaries, lists, and sorting—brute force throughout.
I did not handle the snapshots in level 4 well and did not pass everything. My score was 552/600. I suspect I failed.
I have seen people get rejected even with 600, so I do not know what is going on. I am treating it as practice.
from banking_system import BankingSystem
PAYMENT_PREFIX = 'payment'
class BankingSystemImpl(BankingSystem):
def __init__(self):
self.accounts = {}
self.spenders = {}
self.payments = {}
self.pid = 1
self.snapshots = {}
# ---------- Helpers ----------
def _snapshot(self, timestamp):
self.snapshots[timestamp] = self.accounts.copy()
def settle_scheduled_payments(self, timestamp):
due = [(pid, payment) for pid, payment in self.payments.items()
if payment[2] <= timestamp]
due.sort(key=lambda item: (item[1][2], int(item[0][len(PAYMENT_PREFIX):])))
for pid, (account_id, amount, due_time) in due:
if self.accounts[account_id] >= amount:
self.accounts[account_id] -= amount
self.spenders[account_id] += amount
del self.payments[pid]
self._snapshot(due_time)
self._snapshot(timestamp)
# ---------- Level 1: create_account, deposit, transfer ----------
def create_account(self, timestamp, account_id):
if account_id in self.accounts:
return False
self.accounts[account_id] = 0
self.spenders[account_id] = 0
self._snapshot(timestamp)
return True
def deposit(self, timestamp, account_id, amount):
self.settle_scheduled_payments(timestamp)
if account_id not in self.accounts:
return None
self.accounts[account_id] += amount
self._snapshot(timestamp)
return self.accounts[account_id]
def transfer(self, timestamp, source_account_id, target_account_id, amount):
self.settle_scheduled_payments(timestamp)
if (source_account_id == target_account_id
or source_account_id not in self.accounts
or target_account_id not in self.accounts
or self.accounts[source_account_id] < amount):
return None
self.accounts[source_account_id] -= amount
self.accounts[target_account_id] += amount
self.spenders[source_account_id] += amount
self._snapshot(timestamp)
return self.accounts[source_account_id]
# ---------- Level 2: top_spenders ----------
def top_spenders(self, timestamp, n):
self.settle_scheduled_payments(timestamp)
ranked = sorted(self.spenders.items(), key=lambda p: (-p[1], p[0]))
return [f"{account}({amount})" for account, amount in ranked[:n]]
# ---------- Level 3: schedule_payment, cancel_payment ----------
def schedule_payment(self, timestamp, account_id, amount, delay):
if account_id not in self.accounts:
return None
pid = f"{PAYMENT_PREFIX}{self.pid}"
self.pid += 1
self.payments[pid] = [account_id, amount, timestamp + delay]
return pid
def cancel_payment(self, timestamp, account_id, payment_id):
self.settle_scheduled_payments(timestamp)
payment = self.payments.get(payment_id)
if payment is None or payment[0] != account_id:
return False
del self.payments[payment_id]
return True
# ---------- Level 4: merge_accounts, get_balance ----------
def merge_accounts(self, timestamp, account_id_1, account_id_2):
self.settle_scheduled_payments(timestamp)
if (account_id_1 == account_id_2
or account_id_1 not in self.accounts
or account_id_2 not in self.accounts):
return False
self.accounts[account_id_1] += self.accounts.pop(account_id_2)
self.spenders[account_id_1] += self.spenders.pop(account_id_2)
for payment in self.payments.values():
if payment[0] == account_id_2:
payment[0] = account_id_1
self._snapshot(timestamp)
return True
def get_balance(self, timestamp, account_id, time_at):
self.settle_scheduled_payments(timestamp)
result = None
for t in sorted(self.snapshots):
if t > time_at:
break
result = self.snapshots[t].get(account_id)
return result
Discussion
Loading comments…