Ring buffer

Write codeMedium25 minAmazon and Apple style

Your task

Implement a fixed-capacity ring buffer and process ops: ['write', x] adds x to the back if there is room and returns True, else returns False; ['read'] removes and returns the front value, or -1 if empty; ['overwrite', x] adds x to the back, evicting the oldest element if the buffer is full, and always returns True. Return the list of results for write/read/overwrite ops, in order. capacity >= 1.

Examples

ring_buffer(capacity: int, ops: list[list]) -> list
Example 1
Inputring_buffer(3, [["write", 1], ["write", 2], ["write", 3], ["write", 4], ["read"], ["read"], ["write", 5], ["read"], ["read"], ["read"]])
Output[True, True, True, False, 1, 2, True, 3, 5, -1]