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
Design Circular Queue INPUT 1 [0] 2 [1] 3 [2] front rear Operations: MyCircularQueue(3) enQueue(1), enQueue(2) enQueue(3), enQueue(4) Rear(), isFull() deQueue(), enQueue(4) Rear() Size: k = 3 FIFO Ring Buffer ALGORITHM STEPS 1 Initialize Array Create array[k], front=-1 rear=-1, count=0 2 enQueue Logic if !isFull: rear = (rear+1) % k 3 deQueue Logic if !isEmpty: front = (front+1) % k 4 Wrap Around Modulo connects end to beginning Two Pointer Formula nextIndex = (cur + 1) % k isEmpty: count == 0 isFull: count == k FINAL RESULT 4 2 3 front[1] rear[0] Output Sequence: [null, true, true, true, false, 3, true, true, true, 4] enQueue(4) failed first time (queue full), succeeded after deQueue freed space OK - All Operations Work Space reused via wrap-around Time: O(1) per operation Key Insight: The circular queue uses modulo arithmetic (index % k) to wrap the rear pointer back to position 0 when it reaches the end. This allows reusing freed slots without shifting elements, achieving O(1) for all operations. Track count separately to distinguish empty from full (both have front==rear). TutorialsPoint - Design Circular Queue | Circular Array with Two Pointers
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