Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Design Circular Queue

Queuesmedium

Course · Section 11: Working With Arrays · Lecture 149: Simple Array Methods

Design a fixed-size circular queue. Implement `MyCircularQueue`: - `new MyCircularQueue(k)` capacity k. - `enQueue(v)` / `deQueue()` return whether the operation succeeded. - `Front()` / `Rear()` return the front/rear value or `-1` if empty. - `isEmpty()` / `isFull()`.

Examples

Input: k=3: enQueue(1), enQueue(2), enQueue(3), enQueue(4), Rear(), isFull(), deQueue(), enQueue(4), Rear()
Output: [true, true, true, false, 3, true, 4]
The fourth enQueue fails (full). After deQueue, a new enQueue succeeds and Rear is 4.

Sample tests

Input: fill, overflow, rotate
Output: [true,true,true,false,3,true,4]
Input: empty front
Output: [-1,true]

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Reading Front/Rear without guarding the empty case.
  • Confusing capacity with current count.

Learning resources

  • Wikipedia: Circular buffer
Approach & explanation (try first)

A head pointer plus a count over a fixed array implements a circular buffer; modulo wraps indices. All operations are O(1).

Loading...
⌘/Ctrl + Enter

Run your code to see results.