Design Circular Queue - Problem

Design and implement a circular queue data structure! A circular queue is a clever variation of the traditional queue that connects the last position back to the first position, forming a complete circle.

๐ŸŽฏ The Challenge: Unlike regular queues that waste space when elements are dequeued from the front, circular queues reuse that freed space efficiently. When the rear pointer reaches the end of the array, it wraps around to the beginning if there's available space.

Your Task: Implement the MyCircularQueue class with these operations:

  • MyCircularQueue(k) - Initialize with size k
  • enQueue(value) - Insert element, return true if successful
  • deQueue() - Remove element, return true if successful
  • Front() - Get front element (-1 if empty)
  • Rear() - Get rear element (-1 if empty)
  • isEmpty() - Check if queue is empty
  • isFull() - Check if queue is full

Note: You cannot use built-in queue data structures!

Input & Output

example_1.py โ€” Basic Operations
$ Input: ["MyCircularQueue","enQueue","enQueue","enQueue","enQueue","Rear","isFull","deQueue","enQueue","Rear"] [[3],[1],[2],[3],[4],[],[],[],[4],[]]
โ€บ Output: [null,true,true,true,false,3,true,true,true,4]
๐Ÿ’ก Note: Initialize queue of size 3. Add 1,2,3 successfully. Adding 4 fails (full). Rear is 3. Queue is full. Remove one element. Add 4 successfully. New rear is 4.
example_2.py โ€” Empty Queue Operations
$ Input: ["MyCircularQueue","Front","Rear","deQueue","isEmpty","enQueue","Front"] [[2],[],[],[],[],[5],[]]
โ€บ Output: [null,-1,-1,false,true,true,5]
๐Ÿ’ก Note: Initialize empty queue size 2. Front/Rear return -1 (empty). DeQueue fails (empty). Queue is empty. Add 5. Front is now 5.
example_3.py โ€” Wraparound Behavior
$ Input: ["MyCircularQueue","enQueue","enQueue","deQueue","enQueue","deQueue","enQueue","Front","Rear"] [[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 (uses wrapped position). Remove 2. Add 4. Queue now has [3,4] with front=3, rear=4.

Visualization

Tap to expand
Index 0Index 1Index 2Index 3Index 4102030FRONTREAROperationsenQueue(40): data[rear] = 40 rear = (3+1) % 5 = 4deQueue(): front = (0+1) % 5 = 1 (element 10 removed)Key Benefitsโœ… O(1) all operationsโœ… No element shiftingโœ… Efficient space usageโœ… True circular behaviorWraparound
Understanding the Visualization
1
Initialize
Create circular array with front and rear pointers both at position 0
2
EnQueue
Add element at rear position, move rear pointer clockwise
3
DeQueue
Move front pointer clockwise (element logically removed)
4
Wraparound
Pointers wrap to beginning using modulo when reaching end
Key Takeaway
๐ŸŽฏ Key Insight: Modular arithmetic `(index + 1) % size` creates seamless wraparound, eliminating the need for expensive array shifts while maintaining O(1) performance for all operations.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(1)

All operations (enQueue, deQueue, Front, Rear) execute in constant time

n
2n
โœ“ Linear Growth
Space Complexity
O(k)

Uses array of size k+1 to store k elements with extra space for full/empty detection

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค k โ‰ค 1000
  • 0 โ‰ค value โ‰ค 1000
  • At most 3000 calls will be made to enQueue, deQueue, Front, Rear, isEmpty, and isFull
  • All values will be in the range [0, 1000]
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
87.0K Views
Medium Frequency
~25 min Avg. Time
2.2K 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