Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

In-Memory Banking Operations

HardPython00:00
Practice interviewer
In session
5 left
00:00

Your question is In-Memory Banking Operations. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Implement a transaction processor for Capital One accounts. The processor must support deposits, withdrawals, transfers, and queries for the most active accounts.

An account is identified by a unique string and has a nonnegative integer balance. An account's activity score is the total value of all successful withdrawals and outgoing transfers from that account. Deposits and incoming transfers do not increase the score.

Formal Specification

Implement account_operations(accounts, operations), where accounts is a dictionary mapping account IDs to initial balances, and operations is a list of operation arrays:

  1. ["deposit", account_id, amount]
  2. ["withdraw", account_id, amount]
  3. ["transfer", source_id, destination_id, amount]
  4. ["top", n]

Return one result for every operation that changes state or queries state. Deposit, withdrawal, and transfer return true on success and false otherwise. A transaction fails if an account is unknown, an amount is not positive, or the source balance is insufficient. A transfer must be atomic. A top query returns up to n account IDs sorted by descending activity score, then lexicographically by account ID.

Constraints

  • 1 <= len(accounts) <= 10^5
  • 1 <= len(operations) <= 10^5
  • Initial balances and amounts are integers from 0 through 10^9
  • 1 <= n <= len(accounts)
  • Account IDs are unique strings containing lowercase letters and digits
  • Only accounts present in the initial accounts dictionary may transact

Function Signature

def account_operations(accounts, operations):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output