Challenge - 5 Problems
Enqueue Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Enqueue in Circular Queue
What is the printed state of the queue after these enqueue operations on a circular queue of size 5?
Operations:
Enqueue 10, Enqueue 20, Enqueue 30, Enqueue 40, Enqueue 50
Operations:
Enqueue 10, Enqueue 20, Enqueue 30, Enqueue 40, Enqueue 50
DSA C#
class CircularQueue { private int[] arr; private int front, rear, size, capacity; public CircularQueue(int capacity) { this.capacity = capacity; arr = new int[capacity]; front = 0; rear = -1; size = 0; } public void Enqueue(int item) { if (size == capacity) { Console.WriteLine("Queue is full"); return; } rear = (rear + 1) % capacity; arr[rear] = item; size++; } public string PrintQueue() { if (size == 0) return "Queue is empty"; string result = ""; for (int i = 0; i < size; i++) { int index = (front + i) % capacity; result += arr[index] + " -> "; } return result + "null"; } } var q = new CircularQueue(5); q.Enqueue(10); q.Enqueue(20); q.Enqueue(30); q.Enqueue(40); q.Enqueue(50); Console.WriteLine(q.PrintQueue());
Attempts:
2 left
💡 Hint
Remember that enqueue adds elements at the rear and the queue prints from front to rear.
✗ Incorrect
The circular queue adds elements at the rear index, wrapping around if needed. Since the queue is empty initially, elements are added in order 10, 20, 30, 40, 50. Printing from front to rear shows them in the same order.
❓ Predict Output
intermediateEnqueue Overflow Behavior
What will be the output after trying to enqueue an element into a full queue of capacity 3?
Operations:
Enqueue 1, Enqueue 2, Enqueue 3, Enqueue 4
Operations:
Enqueue 1, Enqueue 2, Enqueue 3, Enqueue 4
DSA C#
class SimpleQueue { private int[] arr; private int front, rear, size, capacity; public SimpleQueue(int capacity) { this.capacity = capacity; arr = new int[capacity]; front = 0; rear = -1; size = 0; } public void Enqueue(int item) { if (size == capacity) { Console.WriteLine("Queue is full"); return; } rear++; arr[rear] = item; size++; } public string PrintQueue() { if (size == 0) return "Queue is empty"; string result = ""; for (int i = front; i <= rear; i++) { result += arr[i] + " -> "; } return result + "null"; } } var q = new SimpleQueue(3); q.Enqueue(1); q.Enqueue(2); q.Enqueue(3); q.Enqueue(4); Console.WriteLine(q.PrintQueue());
Attempts:
2 left
💡 Hint
Check what happens when size equals capacity before adding a new element.
✗ Incorrect
The queue has capacity 3 and already contains 3 elements. Trying to enqueue the 4th element prints "Queue is full" and does not add it. The queue remains with 1, 2, 3.
🔧 Debug
advancedFind the Bug in Enqueue Method
This enqueue method for a queue has a bug. What error will it cause when running?
Code snippet:
Code snippet:
public void Enqueue(int item) {
if (size == capacity) {
Console.WriteLine("Queue is full");
return;
}
rear = (rear + 1) % capacity;
arr[rear] = item;
size++;
}DSA C#
public class Queue { private int[] arr; private int front, rear, size, capacity; public Queue(int capacity) { this.capacity = capacity; arr = new int[capacity]; front = 0; rear = -1; size = 0; } public void Enqueue(int item) { if (size == capacity) { Console.WriteLine("Queue is full"); return; } rear = (rear + 1) % capacity; arr[rear] = item; size++; } public int Dequeue() { if (size == 0) throw new InvalidOperationException("Queue is empty"); int item = arr[front]; front = (front + 1) % capacity; size--; return item; } } var q = new Queue(3); q.Enqueue(10); q.Enqueue(20); q.Enqueue(30); q.Enqueue(40);
Attempts:
2 left
💡 Hint
Check how rear is updated and how size is checked before enqueue.
✗ Incorrect
The enqueue method correctly checks if the queue is full before adding. It updates rear with modulo to wrap around. When full, it prints "Queue is full" and returns. No exception occurs.
🧠 Conceptual
advancedEnqueue Operation Complexity
What is the time complexity of the enqueue operation in a circular queue implemented with an array?
Attempts:
2 left
💡 Hint
Enqueue only updates a few variables and places one element.
✗ Incorrect
Enqueue in a circular queue just moves the rear pointer and inserts the element. These are constant time operations, so complexity is O(1).
🚀 Application
expertResulting Queue After Mixed Operations
Given a circular queue of capacity 4, what is the printed queue after these operations?
Operations:
Enqueue 5, Enqueue 10, Enqueue 15, Dequeue, Enqueue 20, Enqueue 25
Operations:
Enqueue 5, Enqueue 10, Enqueue 15, Dequeue, Enqueue 20, Enqueue 25
DSA C#
class CircularQueue { private int[] arr; private int front, rear, size, capacity; public CircularQueue(int capacity) { this.capacity = capacity; arr = new int[capacity]; front = 0; rear = -1; size = 0; } public void Enqueue(int item) { if (size == capacity) { Console.WriteLine("Queue is full"); return; } rear = (rear + 1) % capacity; arr[rear] = item; size++; } public int Dequeue() { if (size == 0) { Console.WriteLine("Queue is empty"); return -1; } int item = arr[front]; front = (front + 1) % capacity; size--; return item; } public string PrintQueue() { if (size == 0) return "Queue is empty"; string result = ""; for (int i = 0; i < size; i++) { int index = (front + i) % capacity; result += arr[index] + " -> "; } return result + "null"; } } var q = new CircularQueue(4); q.Enqueue(5); q.Enqueue(10); q.Enqueue(15); q.Dequeue(); q.Enqueue(20); q.Enqueue(25); Console.WriteLine(q.PrintQueue());
Attempts:
2 left
💡 Hint
Track front and rear indices carefully after each operation.
✗ Incorrect
Initially enqueue 5,10,15 fills queue: front=0, rear=2, size=3.
Dequeue removes 5, front moves to 1, size=2.
Enqueue 20 at rear=3, size=3.
Enqueue 25 wraps rear to 0, size=4.
Queue elements from front=1: 10,15,20,25.
Dequeue removes 5, front moves to 1, size=2.
Enqueue 20 at rear=3, size=3.
Enqueue 25 wraps rear to 0, size=4.
Queue elements from front=1: 10,15,20,25.
