Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Multithreaded Deposit and Withdrawal
00:00
5 left

Multithreaded Deposit and Withdrawal

HardPython

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):
Interviewer

Your question is Multithreaded Deposit and Withdrawal. Start with the requirements in the Question tab.

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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.