BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Data processing and features30 of 54 in topic

755. Build a bank ledger that grows over four levels

HardData processing and features Pro

This is a level-by-level build, the way timed online assessments often work: get level 1 passing, then extend the same code for each next level without breaking the earlier ones.

Write run_bank(ops). ops is a list of tuples whose second item is a time t; times never go down. Return a list with one output per operation.

Level 1. ('create', t, acct) opens an account with balance 0 and returns True, or False if it already exists. ('deposit', t, acct, amount) adds money and returns the new balance, or None if the account does not exist. ('transfer', t, src, dst, amount) moves money and returns the new balance of src. It returns None (and changes nothing) if either account is missing, src equals dst, or src has less than amount.

Level 2. ('top', t, n) returns up to n strings of the form 'acct(total)', where total is the money that has left that account through transfers out and executed scheduled payments. Sort by total, largest first; break ties by account name in alphabetical order.

Level 3. ('schedule', t, acct, amount, delay) books a payment out of acct due at time t + delay and returns an id 'p1', 'p2', and so on (one counter for the whole bank, counting only successful bookings). It returns None if acct does not exist. Before any operation at time t is handled, every pending payment with due time at most t runs, in order of due time and then booking order. A payment that runs takes the money and counts toward the account total if the balance is high enough; otherwise it is dropped. ('cancel', t, acct, pid) returns True and removes the payment if pid is still pending and belongs to acct, otherwise False.

Level 4. ('merge', t, a, b) folds account b into account a: a gains b's balance and b's outgoing total, b's pending payments now belong to a, and b stops existing. It returns True, or False if a equals b or either account is missing.

Functiondef run_bank(ops)

Example 1

Input: [('create', 1, 'x'), ('create', 2, 'y'), ('deposit', 3, 'x', 100), ('transfer', 4, 'x', 'y', 30), ('transfer', 5, 'y', 'x', 50), ('top', 6, 2)]

Output: [True, True, 100, 70, None, ['x(30)', 'y(0)']]

Example 2

Input: [('create', 1, 'a'), ('deposit', 1, 'a', 20), ('schedule', 2, 'a', 15, 3), ('cancel', 5, 'a', 'p1'), ('deposit', 6, 'a', 0)]

Output: [True, 20, 'p1', False, 5]

Explanation: The payment ran at time 5 before the cancel was handled.

Constraints

  • At most 5,000 operations.
  • Amounts are non-negative integers.
  • Times are non-decreasing integers; delay is at least 1.
Hint 1

Track outgoing totals separately from balances; deposits do not count.

Hint 2

Use a heap of (due, seq, pid) and a dict of live pids for O(log n) scheduling with lazy cancel.

Hint 3

On merge, re-point pending payments from b to a.

Common mistakes
  • Running due payments after the current operation instead of before it.
  • Counting deposits or failed payments in the outgoing total.
  • Letting a failed booking bump the payment id counter.
  • Allowing a transfer from an account to itself.
Understand the idea

Progressive assessments reward code that is easy to extend. Keep the state in a few clear structures, write one branch per operation, and handle cross-cutting rules (like due payments running before anything else at time t) in one place at the top of the loop. When a new level arrives, ask which state it needs and which old operations it changes, rather than patching branches one by one.

Topics
Data processing and features
Similar questions
  • Target-encode a category with smoothingHard
  • Fit imputation, scaling and one-hot encoding on train and apply them to testHard
  • Estimate Jaccard similarity between documents with MinHash signaturesHard
  • Split folds by user so no user lands in both train and validationHard
All Data processing and features problems
Report
755 / 903
Python 3solution.pyPython idle
1
2
3
4
5
def run_bank(ops):
# return one output per operation
out = []
return out
​

Run checks these cases in your browser, no sign-in needed. Submit checks the full hidden test set with Pro.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS