SystemTutor
About
Sign in
Loading course documentation…
SystemTutor

COURSES

  • Data Structures & Algorithms
  • CSS MasterclassNew course
  • System DesignNew course

PRACTICE

  • Algorithm practice
  • CSS practice

LEGAL

  • Privacy Policy
  • Terms of Service
  • About

© 2026 SystemTutor. All rights reserved.

Built with 🫶 by Shatlyk

Course Overview
Singly Linked ListReverse Linked ListDoubly Linked ListData-Structure and Linked-List Trade-offsMerge Two Sorted ListsRemove Nth Node From End of ListStackValid ParenthesesMin StackDaily TemperaturesQueueImplement Queue Using StacksCircular QueuesCircular Queues QuizDesign Circular QueueSliding Window MaximumQueues, Circular Buffers, and Monotonic Deques
Linked Lists, Stacks & Queues

Design Circular Queue

Implement a fixed-capacity queue with circular array indices.

Try It Yourself

Use the editor below to process circular-queue operations.

Keep items in place

Use a fixed-capacity array without shifting existing values. Track the front position and current size. Handle empty, full, capacity-one, and wraparound states.

Submit your implementation when it passes the examples.

Design Circular Queue

mediumQueue · Circular Buffer · Design35 minLC 622
Problem

Process operations on a fixed-capacity circular queue without shifting existing items.

Return one output per operation. The constructor produces null; enqueue and dequeue return booleans; Front, Rear, isEmpty, and isFull return their values.

Example 1:

Input: operations = ["MyCircularQueue", "enQueue", "enQueue", "enQueue", "enQueue", "Rear", "isFull", "deQueue", "enQueue", "Rear"], values = [[3], [1], [2], [3], [4], [], [], [], [4], []]
Output: [null, true, true, true, false, 3, true, true, true, 4]
Explanation: The first three enqueues fill the queue, so adding 4 fails, Rear returns 3, and isFull returns true. Removing 1 frees a slot for 4, which becomes the new rear.

Example 2:

Input: operations = ["MyCircularQueue", "Front", "Rear", "isEmpty"], values = [[2], [], [], []]
Output: [null, -1, -1, true]
Explanation: The constructor returns null. The queue starts empty, so Front and Rear return -1 and isEmpty returns true.

Constraints

  • 1 ≤ capacity ≤ 1,000
  • At most 3,000 operations are performed.
0 attempts

Test results

Results appear here after a run.

Solution

In an eight-slot circular queue with front at 3 and rear at 7, the next insertion wraps to slot 0.

The front index identifies the next item to remove. With capacity k and room available, insert at (front + size) % k. When at least one item is present, read the rear at (front + size - 1) % k. A dequeue advances front by one modulo k and reduces size.

With capacity 3, enqueue 1, 2, 3, dequeue 1, then enqueue 4. The physical slots read [4, 2, 3], but front is 1, so the queue reads 2, 3, 4. Its front is 2 and its rear is 4.

function runCircularQueue(operations, values) {
  class MyCircularQueue {
    constructor(capacity) {
      this.items = Array(capacity);
      this.capacity = capacity;
      this.frontIndex = 0;
      this.size = 0;
    }

    enQueue(value) {
      if (this.isFull()) return false;
      const rearIndex = (this.frontIndex + this.size) % this.capacity;
      this.items[rearIndex] = value;
      this.size++;
      return true;
    }

    deQueue() {
      if (this.isEmpty()) return false;
      this.frontIndex = (this.frontIndex + 1) % this.capacity;
      this.size--;
      return true;
    }

    Front() {
      return this.isEmpty() ? -1 : this.items[this.frontIndex];
    }

    Rear() {
      if (this.isEmpty()) return -1;
      const rearIndex =
        (this.frontIndex + this.size - 1) % this.capacity;
      return this.items[rearIndex];
    }

    isEmpty() {
      return this.size === 0;
    }

    isFull() {
      return this.size === this.capacity;
    }
  }

  let queue;
  const output = [];

  for (let index = 0; index < operations.length; index++) {
    const operation = operations[index];

    if (operation === "MyCircularQueue") {
      queue = new MyCircularQueue(values[index][0]);
      output.push(null);
    } else if (operation === "enQueue") {
      output.push(queue.enQueue(values[index][0]));
    } else {
      output.push(queue[operation]());
    }
  }

  return output;
}

Big O notation

MeasureComplexityExplanation
TimeO(k + n)Initialize k slots, then perform n constant-time queue operations.
Auxiliary spaceO(k)The fixed array stores exactly the queue capacity k.

How is this practice?

Previous lessonCircular Queues QuizNext lessonSliding Window Maximum
Edit on GitHubReport on GitHub
On this page
%

On this page

Try It YourselfSolutionBig O notation