Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Multithreaded Deposit and Withdrawal

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

Your question is Multithreaded Deposit and Withdrawal. 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

Alcatel-Lucent Motive services may receive deposits and withdrawals for many customer accounts at the same time. Implement a thread-safe transaction processor that applies operations concurrently across accounts while preserving the input order of operations belonging to the same account.

A withdrawal succeeds only when the account has sufficient funds. Each deposit or withdrawal must be atomic, so no operation may observe or create a partially updated balance.

Formal Specification

Implement process_transactions(initial_balances, operations). initial_balances is a dictionary mapping account IDs to nonnegative integer balances. operations is a list of dictionaries with keys account, type, and amount, where type is either "deposit" or "withdraw", and amount is a positive integer.

Return a dictionary with balances, the final balance for every account, and results, a Boolean list aligned with operations. A deposit always succeeds. A withdrawal returns true and updates the balance when funds are sufficient; otherwise it returns false and leaves the balance unchanged.

Use multiple Python threads. Operations for different accounts should be able to execute concurrently. Operations for the same account must be applied in their original input order.

Constraints

  • 1 <= len(initial_balances) <= 1,000
  • 1 <= len(operations) <= 100,000
  • Every operation references an account in initial_balances
  • Initial balances and amounts are integers from 1 through 10^12
  • Operations for each account must retain their input order

Function Signature

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