Your question is Fixed Size Ring Buffer. 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.
Deliveroo services may need a bounded in-memory queue for short-lived order events. Implement a fixed-capacity ring buffer that processes commands in constant time while preserving FIFO order.
Write process_ring_buffer(capacity, operations). The buffer initially contains no items. Each operation is represented as a list:
["enqueue", value]: add value to the back. Return true if successful, or false if the buffer is full.["dequeue"]: remove and return the oldest value, or return null if empty.["peek"]: return the oldest value without removing it, or null if empty.["size"]: return the current number of stored values.Return an array containing one result for every operation. The implementation must use a fixed-size array internally, reuse positions after wraparound, and must not shift elements during removal.
def process_ring_buffer(capacity, operations):