Code Room
CodingMediumcod-g340
Subject Circular bufferLevel Mid–Senior~25 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

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.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.