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.
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.
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.
def process_transactions(initial_balances, operations):