17 · Queues & Deques

#622 Design Circular Queue

Mediumsolve on LeetCode ↗

frequently reported atAppleAmazonMicrosoftMeta

the problem

In plain words

Implement a fixed-capacity FIFO queue MyCircularQueue(k) with enQueue, deQueue (return true/false), Front, Rear (return -1 if empty), isEmpty and isFull, reusing the freed space at the front.

in k = 3; enQueue(1), enQueue(2), enQueue(3), enQueue(4), Rear(), isFull(), deQueue(), enQueue(4), Rear()

out true, true, true, false, 3, true, true, true, 4

The 4th enQueue fails (full). After one deQueue, 4 fits into the freed slot at index 0.

  • 1 ≤ k ≤ 1000
  • 0 ≤ value ≤ 1000
  • At most 3000 calls

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

Design: implement every operation. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #641
      Design Circular Deque
      Mediumin Queues
    2. #232
    3. #362
      Design Hit Counter
      Mediumin QueuesLeetCode Premium