Design Circular Queue - Problem

Design your implementation of the circular queue. The circular queue is a linear data structure in which operations are performed based on FIFO (First In First Out) principle, and the last position is connected back to the first position to make a circle. It is also called "Ring Buffer".

One of the benefits of the circular queue is that we can make use of the spaces in front of the queue. In a normal queue, once the queue becomes full, we cannot insert the next element even if there is a space in front of the queue. But using the circular queue, we can use the space to store new values.

Implement the MyCircularQueue class:

  • MyCircularQueue(k) - Initializes the object with the size of the queue to be k
  • boolean enQueue(int value) - Inserts an element into the circular queue. Return true if the operation is successful
  • boolean deQueue() - Deletes an element from the circular queue. Return true if the operation is successful
  • int Front() - Gets the front item from the queue. If the queue is empty, return -1
  • int Rear() - Gets the last item from the queue. If the queue is empty, return -1
  • boolean isEmpty() - Checks whether the circular queue is empty or not
  • boolean isFull() - Checks whether the circular queue is full or not

You must solve the problem without using the built-in queue data structure in your programming language.

Input & Output

Example 1 — Basic Queue Operations
$ Input: operations = ["MyCircularQueue", "enQueue", "enQueue", "enQueue", "enQueue", "Rear", "isFull", "deQueue", "enQueue", "Rear"], values = [[3], [1], [2], [3], [4], [], [], [], [4], []]
Output: [null, true, true, true, false, 3, true, true, true, 4]
💡 Note: Create queue of size 3, add 1,2,3 successfully, adding 4 fails (full), rear is 3, queue is full, dequeue removes 1, now can add 4, new rear is 4
Example 2 — Empty Queue Checks
$ Input: operations = ["MyCircularQueue", "isEmpty", "Front", "Rear"], values = [[2], [], [], []]
Output: [null, true, -1, -1]
💡 Note: Create empty queue of size 2, isEmpty returns true, Front and Rear return -1 for empty queue
Example 3 — Wrap Around Behavior
$ Input: operations = ["MyCircularQueue", "enQueue", "enQueue", "deQueue", "enQueue", "deQueue", "enQueue", "Front", "Rear"], values = [[2], [1], [2], [], [3], [], [4], [], []]
Output: [null, true, true, true, true, true, true, 3, 4]
💡 Note: Size 2 queue: add 1,2 → remove 1 → add 3 → remove 2 → add 4. Shows circular reuse of positions

Constraints

  • 1 ≤ k ≤ 1000
  • 0 ≤ value ≤ 1000
  • At most 3000 calls will be made to enQueue, deQueue, Front, Rear, isEmpty, and isFull

Visualization

Tap to expand
Circular Queue: Efficient Ring Buffer ImplementationInput Operations:["MyCircularQueue", "enQueue", "enQueue", "deQueue"]Values: [[3], [1], [2], []]Execute12frontrearSize: 3, Count: 2Output Results:[null, true, true, true]
Understanding the Visualization
1
Input
Sequence of queue operations and their parameters
2
Process
Execute operations using circular array with front/rear pointers
3
Output
Return results of each operation
Key Takeaway
🎯 Key Insight: Modular arithmetic enables efficient circular indexing without element shifting
Asked in
Amazon 45 Microsoft 35 Google 30 Facebook 25
142.0K Views
Medium Frequency
~25 min Avg. Time
1.6K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen