SDE Path

Circular Queue

Easy

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 k is fixed at construction; the queue never holds more than k elements.
  • Every operation runs in O(1) time and the backing storage stays O(k) — do not shift elements on dequeue.
  • enqueue on a full queue returns false and must not overwrite a live element; dequeue and front on 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

Example 1
Input
14
3
enqueue 1
enqueue 2
enqueue 3
enqueue 4
front
isFull
dequeue
enqueue 4
front
dequeue
dequeue
dequeue
isEmpty
dequeue
Expected output
true
true
true
false
1
true
1
true
2
2
3
4
true
-1
Example 2
Input
13
2
isEmpty
front
dequeue
enqueue 7
isEmpty
isFull
enqueue 8
isFull
front
dequeue
front
dequeue
front
Expected output
true
-1
-1
true
false
false
true
true
7
7
8
8
-1