Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Preventing Buffer Race Conditions

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

Your question is Preventing Buffer Race Conditions. 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 do you prevent race conditions when sharing a buffer between a main loop and an interrupt handler?

Implement process_buffer(capacity, operations) to simulate a bounded single-producer, single-consumer ring buffer. Each operation is ['write', value] or ['read']; return True or False for writes and the consumed integer or None for reads. Writes to a full buffer and reads from an empty buffer must fail without corrupting existing data.

Constraints

  • 1 <= capacity <= 10^4
  • 1 <= operations.length <= 10^5
  • Each operation is either ['write', value] or ['read']
  • write values are integers
  • Operations are presented in the order in which the producer and consumer access the buffer

Function Signature

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