Skip to content

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.

Medium#622
LeetCode Problem

Design Circular Queue

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

Input
["MyCircularQueue","enQueue","enQueue","enQueue","deQueue","enQueue","Front","Rear","isFull"]
[[2],[4],[7],[9],[],[9],[],[],[]]
Output
[null,true,true,false,true,true,7,9,true]
Explanation

Capacity two rejects 9 while full. Removing 4 creates room; reinserting 9 wraps the ring and preserves front 7.

Constraints

1 <= k <= 1000
0 <= value <= 1000
At most 3000 calls across enQueue, deQueue, Front, Rear, isEmpty, and isFull.

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.