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.
capacity, and a list operations where each item is either ["push", value], ["pop"], ["peek"], or ["size"]Example 1
capacity = 3, operations = [["push", 10], ["push", 20], ["peek"], ["pop"], ["size"]][true, true, 10, 10, 1]10 is at the front, then removed, leaving one element.Example 2
capacity = 2, operations = [["pop"], ["push", 1], ["push", 2], ["push", 3], ["size"], ["pop"], ["pop"], ["pop"]][-1, true, true, false, 2, 1, 2, -1]1 <= capacity <= 10^51 <= len(operations) <= 2 * 10^5-10^9 <= value <= 10^9O(1) time each