Circular buffer
Implement a fixed-capacity circular buffer (ring) of size `cap`. Operations: ['enqueue', x] inserts at the rear and returns True, or False if full; ['dequeue'] removes from the front and returns the value, or None if empty; ['front'] returns the front value or None; ['rear'] returns the rear value or None. Given cap and an op list, return the list of results for enqueue/dequeue/front/rear (in op order). Use None for empty results.
Implement
circular_buffer(cap: int, ops: list[list]) → listExamples
in
[3,[["enqueue",1],["enqueue",2],["enqueue",3],["enqueue",4],["front"],["dequeue"],["enqueue",4],["rear"]]]out[true,true,true,false,1,1,true,4]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 20 min
solution.py
InputExpectedGot
[3,[["enqueue",1],["enqueue",2],["enqueue",3],["enqueue",4],["front"],["dequeue"],["enqueue",4],["rear"]]][true,true,true,false,1,1,true,4]not run yetsample