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.
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.
def process_buffer(capacity, operations):