Your question is Circular Buffer for Embedded. 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.
An Amazon Echo device uses a fixed-size memory region to buffer events before they are processed. Implement a circular buffer with overwrite-oldest behavior: when a push occurs on a full buffer, discard the oldest element and retain the new one.
Implement process_circular_buffer(capacity, operations). Each operation is an array whose first element is a command:
["push", value]: insert one integer.["push_many", values]: insert all integers in order.["pop"]: remove and return the oldest value, or None if empty.["peek"]: return the oldest value without removing it, or None if empty.["snapshot"]: return all values from oldest to newest.["size"]: return the current number of stored values.["is_full"]: return whether the buffer is full.Return an array containing the result of every operation that produces a value: pop, peek, snapshot, size, and is_full. Push operations produce no result.
def process_circular_buffer(capacity, operations):