Tutorialspoint
Problem
Solution
Submissions

Circular Queue with Dynamic Resizing

Certification: Intermediate Level Accuracy: 0% Submissions: 1 Points: 12

Write a C# program to implement a circular queue with dynamic resizing. A circular queue is a linear data structure that follows the FIFO (First In First Out) principle. The operations should include Enqueue, Dequeue, Front, Rear, IsEmpty, IsFull, and resize when needed.

Example 1
  • Input:
    MyCircularQueue q = new MyCircularQueue(3);
    q.Enqueue(1);
    q.Enqueue(2);
    q.Enqueue(3);
    q.Enqueue(4);
    q.Dequeue();
    q.Enqueue(5);
    q.Front();
  • Output:
    true // Enqueue 1
    true // Enqueue 2
    true // Enqueue 3
    true // Enqueue 4 (triggers resize)
    1 // Dequeue returns 1
    true // Enqueue 5
    2 // Front value
Example 2
  • Input:
    MyCircularQueue q = new MyCircularQueue(5);
    q.Enqueue(10);
    q.Enqueue(20);
    q.Dequeue();
    q.Dequeue();
    q.IsEmpty();
  • Output:
    true // Enqueue 10
    true // Enqueue 20
    10 // Dequeue returns 10
    20 // Dequeue returns 20
    true // queue is empty
Constraints
  • 1 <= capacity <= 1000
  • 0 <= value <= 10^4
  • At most 3000 calls will be made to Enqueue, Dequeue, Front, Rear, IsEmpty, and IsFull
  • Time Complexity: O(1) average time per operation, O(n) for resize
  • Space Complexity: O(n), where n is the capacity of the queue
QueuePwCSwiggy
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use an array to implement the circular queue
  • Keep track of front and rear indices
  • Implement Enqueue, Dequeue, Front, Rear, IsEmpty, and IsFull operations
  • When the queue is full, resize it by creating a new array with double capacity
  • Properly handle wrapping around the end of the array

Steps to solve by this approach:

 Step 1: Initialize the circular queue with specified capacity, front and rear indices, and size.
 Step 2: Implement Enqueue operation that adds elements to the rear of the queue and handles wraparound.
 Step 3: Implement Dequeue operation that removes elements from the front of the queue and handles wraparound.
 Step 4: Implement Front and Rear operations to access the respective elements without removing them.
 Step 5: Implement IsEmpty and IsFull operations to check the queue's state.
 Step 6: Implement the Resize operation that doubles the capacity when the queue is full.
 Step 7: When resizing, handle proper copying of elements to maintain their order in the new array.

Submitted Code :