Circular Queue
Design a fixed-capacity queue backed by a ring buffer so every operation is O(1) and the storage is reused as elements wrap around.
Requirements
- The capacity
kis fixed at construction; the queue never holds more thankelements. - Every operation runs in O(1) time and the backing storage stays O(k) — do not shift elements on
dequeue. enqueueon a full queue returnsfalseand must not overwrite a live element;dequeueandfronton an empty queue return-1.
API to implement
Implement the class CircularQueue. The I/O driver is already written in the starter code — it parses the command script below and calls your methods. You only implement the class.
| Command | Returns | Behavior |
|---|---|---|
| enqueue <val> | boolean | Insert val at the rear. Return false if the queue is already full, otherwise true. |
| dequeue | int | Remove the front element and return it, or -1 if the queue is empty. |
| front | int | Return the front element without removing it, or -1 if the queue is empty. |
| isFull | boolean | Return whether the queue is at capacity. |
| isEmpty | boolean | Return whether the queue currently holds no elements. |
Input format
The first line contains Q, the number of operations. The second line contains the constructor argument: k — the fixed capacity of the queue. Each of the next Q lines is one command as listed above. String arguments contain no spaces.
Output format
For each command with a non-void return, print its result on its own line, in order. The driver does this for you — your methods just return values.
This is a design problem: the hidden tests exercise edge cases and interaction sequences, not performance tricks. Model the state cleanly and the tests will pass.
Sample cases
14
3
enqueue 1
enqueue 2
enqueue 3
enqueue 4
front
isFull
dequeue
enqueue 4
front
dequeue
dequeue
dequeue
isEmpty
dequeuetrue
true
true
false
1
true
1
true
2
2
3
4
true
-113
2
isEmpty
front
dequeue
enqueue 7
isEmpty
isFull
enqueue 8
isFull
front
dequeue
front
dequeue
fronttrue
-1
-1
true
false
false
true
true
7
7
8
8
-1