Code RoomRing buffer
MediumPrep Room Coding #910

Ring buffer

CodingAlgorithms & data structuresMid–Senior~25 min

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.

Implement
ring_buffer(capacity: int, ops: list[list]) → list
Examples
in[3,[["write",1],["write",2],["write",3],["write",4],["read"],["read"],["write",5],["read"],["read"],["read"]]]out[true,true,true,false,1,2,true,3,5,-1]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 25 min
InputExpectedGot
[3,[["write",1],["write",2],["write",3],["write",4],["read"],["read"],["write",5],["read"],["read"],["read"]]][true,true,true,false,1,2,true,3,5,-1]not run yetsample