> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Design Circular Queue Python Solution

> Tested Python solution for LeetCode 622 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 622, Medium. Topics: Array, Linked List, Design, Queue. [View on LeetCode](https://leetcode.com/problems/design-circular-queue/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 622   # by problem number
lcpy gen -s design_circular_queue   # by problem name
```

## Problem

Design your implementation of the circular queue. The circular queue is a linear data structure that operates on the FIFO (First In First Out) principle, with the last position connected back to the first to form a circle (a "Ring Buffer").

Implement the `MyCircularQueue` class:

* `MyCircularQueue(k)` Initializes the object with the queue size `k`.
* `int Front()` Gets the front item; returns `-1` if empty.
* `int Rear()` Gets the last item; returns `-1` if empty.
* `boolean enQueue(int value)` Inserts an element. Returns `true` if successful.
* `boolean deQueue()` Deletes an element from the queue. Returns `true` if successful.
* `boolean isEmpty()` Checks whether the queue is empty.
* `boolean isFull()` Checks whether the queue is full.

You must solve the problem without using the built-in queue data structure.

### Examples

```
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]

Explanation
myCircularQueue = MyCircularQueue(3);
myCircularQueue.enQueue(1);  // True
myCircularQueue.enQueue(2);  // True
myCircularQueue.enQueue(3);  // True
myCircularQueue.enQueue(4);  // False, queue is full
myCircularQueue.Rear();      // 3
myCircularQueue.isFull();    // True
myCircularQueue.deQueue();   // True
myCircularQueue.enQueue(4);  // True
myCircularQueue.Rear();      // 4
```

### Constraints

* 1 \<= k \<= 1000
* 0 \<= value \<= 1000
* At most 3000 calls will be made to enQueue, deQueue, Front, Rear, isEmpty, and isFull.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/design_circular_queue/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/design_circular_queue/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class MyCircularQueue:
    # Time: O(1) per operation
    # Space: O(k)
    def __init__(self, k: int) -> None:
        self.capacity = k
        self.data: list[int] = [0] * k
        self.head = 0
        self.size = 0

    # Time: O(1)
    # Space: O(1)
    def en_queue(self, value: int) -> bool:
        if self.is_full():
            return False
        tail_index = (self.head + self.size) % self.capacity
        self.data[tail_index] = value
        self.size += 1
        return True

    # Time: O(1)
    # Space: O(1)
    def de_queue(self) -> bool:
        if self.is_empty():
            return False
        self.head = (self.head + 1) % self.capacity
        self.size -= 1
        return True

    # Time: O(1)
    # Space: O(1)
    def front(self) -> int:
        if self.is_empty():
            return -1
        return self.data[self.head]

    # Time: O(1)
    # Space: O(1)
    def rear(self) -> int:
        if self.is_empty():
            return -1
        tail_index = (self.head + self.size - 1) % self.capacity
        return self.data[tail_index]

    # Time: O(1)
    # Space: O(1)
    def is_empty(self) -> bool:
        return self.size == 0

    # Time: O(1)
    # Space: O(1)
    def is_full(self) -> bool:
        return self.size == self.capacity
```

## Complexity

| Time               | Space |
| ------------------ | ----- |
| O(1) per operation | O(k)  |

## Tags

[NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
