Your question is Thread-Safe Circular Buffer Operations. 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.
In Meta embedded systems, components such as sensor pipelines or device telemetry often use a fixed-size circular buffer to pass data between producers and consumers. Implement the core buffer operations for a bounded circular buffer and return the results of a sequence of commands.
Write a function that simulates a circular buffer of capacity capacity. The buffer stores integers and supports these commands in order:
"push", value — insert value at the tail if the buffer is not full; otherwise return false for that command."pop" — remove and return the oldest value if the buffer is not empty; otherwise return -1."peek" — return the oldest value without removing it; return -1 if empty."size" — return the current number of elements.Use a fixed-size array with wraparound indexing. The goal is to model the core logic that a thread-safe C circular buffer would protect with a mutex, while keeping this interview problem focused on correct buffer behavior.
def circular_buffer_results(capacity, operations):