Level 1 Fixed-size circular queue
Implement CircularQueue(capacity), a FIFO queue that holds at most capacity items (capacity >= 1). Store the items in a Python list of length capacity allocated once in __init__, and track the front index and the size yourself. Don't use collections.deque, list.pop(0), insert(0, ...) or slicing to shift items.
Methods:
enqueue(value) -> bool: add at the back; returnFalse(and change nothing) if the queue is full.dequeue(): remove and return the front item; returnNoneif the queue is empty.front()/rear(): the first / last item without removing it, orNoneif empty.is_empty() -> bool,is_full() -> bool, andlen(q).
q = CircularQueue(2)
q.enqueue(1) # True
q.enqueue(2) # True
q.enqueue(3) # False (full)
q.dequeue() # 1
q.enqueue(3) # True -- wraps around to slot 0
q.rear() # 3
len(q) # 2
The classic bug is telling "full" from "empty" when the front and back indices coincide. Keeping an explicit size avoids it. Values can be falsy, such as 0 or "", so never test a slot for truthiness.