A fixed-size circular buffer: the structure behind log tails, audio buffers and metric
windows. Writing to a full buffer overwrites the oldest value.
solve(args, ops) builds new RingBuffer(capacity) — args = [capacity] — and replaysops:
write(value) — append value; if the buffer is full, drop the oldest value first.Returns nothing.
read() — remove and return the oldest value, or null if the buffer is empty.snapshot() — return all values as an array, oldest first, without removing them.size() — the number of values currently stored.All operations except snapshot must be O(1): allocate the array once and move a start
index and a size counter around it with modulo arithmetic — no shift().
Example (capacity = 3)
write(1) write(2) write(3) write(4) snapshot() → [2,3,4] // 1 was overwritten
read() → 2Sample tests