Design a class RingBuffer: a first-in-first-out queue of integers with a fixed capacity, backed by a fixed-size list (do not use collections.deque or list pop(0)).
RingBuffer(capacity): create an empty buffer that can holdcapacityitems.enqueue(x): addxat the back; returnTrueon success,Falseif the buffer is full.dequeue(): remove the front item and returnTrue, or returnFalseif the buffer is empty.front(): return the front item, or-1if empty.rear(): return the back item, or-1if empty.is_empty(),is_full(): return booleans.
Examples
Input: ops = ["RingBuffer", "enqueue", "enqueue", "enqueue", "enqueue", "rear", "is_full", "dequeue", "enqueue", "rear", "front"]
args = [[3], [1], [2], [3], [4], [], [], [], [4], [], []]
Output: [None, True, True, True, False, 3, True, True, True, 4, 2]
Explanation: the fourth enqueue fails because the buffer holds 3; after removing 1 there is room for 4.
Constraints
1 <= capacity <= 1000- At most
10**4operations - Target:
O(1)per operation
Goals
- Implement a queue on top of a fixed-size list using head index and count
- Wrap indices around with modulo
- Distinguish the full and empty states