Advertisement

Design Circular Queue - LeetCode 622 Solution

Design Circular Queue - Complete Solution Guide

Design Circular Queue is LeetCode problem 622, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Design your implementation of the circular queue. The circular queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle, and the last position is connected back to the first position to make a circle. It is also called "Ring Buffer". One of the benefits of the circular queue is that we can make use of the spaces in front of the queue. In a normal queue, once the queue becomes full, we cannot insert the next element even if there is a spa

Detailed Explanation

The problem asks us to implement a circular queue data structure without using built-in queue classes. A circular queue is a queue that reuses space at the beginning of the queue after elements are dequeued. It behaves like a regular FIFO (First-In, First-Out) queue, but when the 'tail' reaches the end of the allocated space, it wraps around to the beginning, effectively using the available empty slots. We need to implement methods for initialization, enqueue (add element), dequeue (remove element), get front element, get rear element, check if the queue is empty, and check if the queue is full.

Solution Approach

The solution uses an array to store the queue elements, along with two pointers, `head` and `tail`, to track the front and rear of the queue. The `capacity` of the queue is initialized to `k + 1`, where `k` is the specified queue size, to differentiate between empty and full states. The modulo operator (%) is used extensively to ensure that the `head` and `tail` wrap around the array when they reach the end. The `enQueue` operation adds an element at the `tail` position and increments `tail` using modulo. The `deQueue` operation increments the `head` using modulo. `Front` and `Rear` return the elements at the `head` and `tail - 1` positions, respectively. `isEmpty` and `isFull` check the conditions based on the positions of `head` and `tail`.

Step-by-Step Algorithm

  1. Step 1: **Initialization (MyCircularQueue(k))**: Create an array of size `k + 1`, initialize `head` and `tail` to 0, and set `capacity` to `k + 1`.
  2. Step 2: **Enqueue (enQueue(value))**: If the queue is full, return `false`. Otherwise, add the `value` at `queue[tail]`, and update `tail` as `tail = (tail + 1) % capacity`. Return `true`.
  3. Step 3: **Dequeue (deQueue())**: If the queue is empty, return `false`. Otherwise, increment `head` as `head = (head + 1) % capacity`. Return `true`.
  4. Step 4: **Front (Front())**: If the queue is empty, return -1. Otherwise, return `queue[head]`.
  5. Step 5: **Rear (Rear())**: If the queue is empty, return -1. Otherwise, calculate the rear index as `rear_index = (tail - 1 + capacity) % capacity` and return `queue[rear_index]`.
  6. Step 6: **IsEmpty (isEmpty())**: Return `true` if `head == tail`, and `false` otherwise.
  7. Step 7: **IsFull (isFull())**: Return `true` if `(tail + 1) % capacity == head`, and `false` otherwise.

Key Insights

  • Insight 1: The key to implementing a circular queue efficiently is using the modulo operator (%) to handle the wrap-around effect when the head or tail reaches the end of the underlying array.
  • Insight 2: To distinguish between an empty and a full queue when both the head and tail are at the same position, it's useful to allocate one extra space in the underlying array. This allows us to determine fullness by checking if `(tail + 1) % capacity == head`.
  • Insight 3: Implementing the `Rear()` method requires handling the case where the `tail` is 0 (meaning it has wrapped around), requiring an adjustment to calculate the correct index using `(tail - 1 + capacity) % capacity`.

Complexity Analysis

Time Complexity: O(1)

Space Complexity: O(k)

Topics

This problem involves: Array, Linked List, Design, Queue.

Companies

Asked at: Applied Intuition, Citadel, Cloudflare, Datadog, Intuit, Qualcomm, Tesla, Zoox.