#622

Design Circular Queue

Medium Array ring buffer

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Implement a fixed-size circular queue with enQueue, deQueue, Front, Rear, isEmpty and isFull.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: a fixed-size queue that wraps around an array.

Why this works: keep head and count; the next free slot is (head + count) % capacity.

Example: capacity 3: add 1, 2, 3, remove, add 4

[1, 2, 3], head 0remove → head 1add 4 at (1 + 2) % 3 = 0 → [4, 2, 3]

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Tracking only head and tail without a count — empty and full look the same.

Solve on LeetCode ↗
Complexity
  • Time: O(1) per operation.
  • Space: O(k).
Topic: Stack & Queue
🎬 Episodes 46–52: video coming soon