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 kenQueue(value)- Insert element, return true if successfuldeQueue()- Remove element, return true if successfulFront()- Get front element (-1 if empty)Rear()- Get rear element (-1 if empty)isEmpty()- Check if queue is emptyisFull()- Check if queue is full
Note: You cannot use built-in queue data structures!
Input & Output
Visualization
Time & Space Complexity
All operations (enQueue, deQueue, Front, Rear) execute in constant time
Uses array of size k+1 to store k elements with extra space for full/empty detection
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]