↓Skip to main content
  1. LeetCode/

LeetCode 622: Design Circular Queue

·2 mins· ·
LeetCode Medium Queue Design Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-09-14

  • Submissions: Accepted (first try)

Approach
#

A circular queue uses a fixed array of size k and reuses space from the start once the end is reached.

Two variables are enough:

  • head: the position of the first element.
  • count: the number of elements.

Every other position can be computed:

PositionFormula
Next write position(head + count) % k
Last element(head + count - 1) % k
New head after a dequeue(head + 1) % k

Deciding empty and full with count avoids the “is head == tail empty or full?” ambiguity.

My Solution
#

class MyCircularQueue:

    def __init__(self, k: int):
        self.buf = [0] * k
        self.head = 0
        self.count = 0

    def enQueue(self, value: int) -> bool:
        if self.isFull():
            return False
        self.buf[(self.head + self.count) % len(self.buf)] = value
        self.count += 1
        return True

    def deQueue(self) -> bool:
        if self.isEmpty():
            return False
        self.head = (self.head + 1) % len(self.buf)
        self.count -= 1
        return True

    def Front(self) -> int:
        return -1 if self.isEmpty() else self.buf[self.head]

    def Rear(self) -> int:
        return -1 if self.isEmpty() else self.buf[(self.head + self.count - 1) % len(self.buf)]

    def isEmpty(self) -> bool:
        return self.count == 0

    def isFull(self) -> bool:
        return self.count == len(self.buf)

Complexity
#

Every operation is O(1); space O(k).

Mistakes to Avoid
#

  • Using head and tail pointers without a count: they are equal both when empty and when full.
  • Forgetting the - 1 for Rear.
  • Clearing array values on dequeue: unnecessary, since later writes overwrite them.

Interview Explanation
#

I use an array of size k with head, the position of the first element, and count, the number of elements. Enqueue writes at (head + count) % k, dequeue advances head modulo k, and the last element is at (head + count - 1) % k. Empty and full are read directly from count, so there is no ambiguity when pointers coincide. Every operation is O(1).

Related

LeetCode 3829: Design Ride Sharing System
·2 mins
LeetCode Medium Queue Design Hash-Map Lazy-Deletion
LeetCode 3829 note: one queue each for riders and drivers; cancellations only update a set, and canceled riders at the front are dropped before matching
LeetCode 307: Range Sum Query - Mutable
·3 mins
LeetCode Medium Segment-Tree Binary-Indexed-Tree Design Array
LeetCode 307 note: point updates with range sums make prefix sums O(n) per update; a segment tree or Fenwick tree makes both operations O(log n)
LeetCode 281: Zigzag Iterator
·2 mins
LeetCode Medium Queue Design Iterator
LeetCode 281 note: a queue of unfinished sources, take one value in turn and put the source back if anything remains; it generalizes to k sources
LeetCode 1570: Dot Product of Two Sparse Vectors
·2 mins
LeetCode Medium Two-Pointers Design Hash-Table Array
LeetCode 1570 note: store only non-zero (index, value) pairs, and compute the dot product by merging the two index-sorted lists with two pointers, multiplying only on equal indices
LeetCode 3835: Count Subarrays With Cost Less Than or Equal to K
·2 mins
LeetCode Medium Sliding-Window Monotonic-Queue Queue Array
LeetCode 3835 note: cost = (max - min) × length only grows as the window grows, so a sliding window counts subarrays; two monotonic deques keep the window’s max and min
LeetCode 244: Shortest Word Distance II
·2 mins
LeetCode Medium Two-Pointers Design Hash-Table String Array
LeetCode 244 note: precompute each word’s positions (sorted by construction), then answer each query by merging the two position lists with two pointers, always advancing the smaller one