Practice Design Circular Queue
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
Implement MyCircularQueue(k) as a fixed-capacity FIFO ring buffer without a built-in queue. enQueue(value) appends unless full and returns success. deQueue() removes the front unless empty and returns success. Front() and Rear() return the corresponding value or -1 when empty. isEmpty() and isFull() report capacity state. Practice approach: Store an array, front index, and entry count. Compute insertion at (front + count) modulo k; removal advances front modulo k. Count distinguishes empty from full even when indices coincide. All operations take O(1) with O(k) storage. Test wraparound, capacity one, and failed insertions without mutation.
Source: Design Circular Queue on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
["MyCircularQueue","enQueue","enQueue","enQueue","deQueue","enQueue","Front","Rear","isFull"] [[2],[4],[7],[9],[],[9],[],[],[]]
[null,true,true,false,true,true,7,9,true]
Capacity two rejects 9 while full. Removing 4 creates room; reinserting 9 wraps the ring and preserves front 7.
Constraints
Related LeetCode Problems
Practice more problems similar to Design Circular Queue.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.