Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Race Conditions in Python

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

Your question is Race Conditions in Python. 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

How would you handle race conditions in Python?

Implement thread_safe_total(increments), where each list element is the number of times one worker thread must increment a shared counter. Protect the compound read-modify-write operation so the returned total is deterministic.

Input and Output

increments is a list of non-negative integers. Return one integer equal to the sum of all elements after every worker completes.

Constraints

  • 0 <= len(increments) <= 100
  • 0 <= increments[i] <= 10000
  • Every worker must complete before the function returns
  • The returned value must equal sum(increments)

Function Signature

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