dsa · easy

Design Circular Queue

GreyOrangeQueueDesignFoundation

Implement a circular queue: a FIFO of **fixed** capacity k that reuses slots after dequeue instead of shifting every element.

Construct with MyCircularQueue(k). It holds at most k integers.

Methods

Keep a head index and a count (or head + tail). Wrap with modulo k. Do not scan the buffer and do not grow it.

Example

Capacity 3 — watch the wrap into slot 0.

`` MyCircularQueue(3) empty [_, _, _] enQueue(1) → true [1, _, _] front=1 rear=1 enQueue(2) → true [1, 2, _] front=1 rear=2 enQueue(3) → true [1, 2, 3] full enQueue(4) → false still full Rear() → 3 isFull() → true deQueue() → true [_, 2, 3] front=2 rear=3 enQueue(4) → true [4, 2, 3] 4 wraps into slot 0; rear=4 Rear() → 4 ``

Fill in the MyCircularQueue class. The starter already walks ops / args and calls your methods — leave the driver at the bottom as-is. Constructors contribute null; booleans print as true / false.

Constraints

1 <= k <= 1000, calls <= 1000

Examples

Example 1

Input:
["MyCircularQueue","enQueue","enQueue","enQueue","enQueue","Rear","isFull","deQueue","enQueue","Rear"]
[[3],[1],[2],[3],[4],[],[],[],[4],[]]

Expected:
[null,true,true,true,false,3,true,true,true,4]

Open in the Dojo editor