Design Circular Queue
Design a circular queue of fixed capacity k. enQueue(value) inserts at the rear and returns true, or false if the queue is full. deQueue() removes the front element and returns true, or false if empty. Front() and Rear() return the front and rear values or -1 if empty. isEmpty() and isFull() report the state. The circular design reuses freed slots at the front instead of wasting space.
Open official problem prompt ↗Implement a fixed-capacity FIFO queue that reuses freed front slots, with every operation in constant time.
Like a circular parking lot with numbered spots: cars leave from the front and new cars fill the next open spot, and the counting wraps back to spot 0 after the last one.
- Input
- MyCircularQueue(3); enQueue(1); enQueue(2); enQueue(3); enQueue(4); Rear(); isFull(); deQueue(); enQueue(4); Rear()
- Output
- [null, true, true, true, false, 3, true, true, true, 4]
- Why
- The 4th enQueue fails (full, capacity 3); after one deQueue frees a slot, enQueue(4) succeeds and Rear() is 4.
1 <= k <= 10000 <= value <= 1000At most 3000 calls across all methodsAll operations must run in O(1) time