Challenge - 5 Problems
Queue Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after enqueue and dequeue operations?
Consider a queue implemented using an array with a fixed size of 5. Initially, the queue is empty. The following operations are performed in order:
Enqueue 10, Enqueue 20, Enqueue 30, Dequeue, Enqueue 40, Enqueue 50, Dequeue, Enqueue 60.
What is the state of the queue after these operations?
Enqueue 10, Enqueue 20, Enqueue 30, Dequeue, Enqueue 40, Enqueue 50, Dequeue, Enqueue 60.
What is the state of the queue after these operations?
DSA C#
class Queue { private int[] arr = new int[5]; private int front = 0; private int rear = 0; private int count = 0; public void Enqueue(int val) { if (count == arr.Length) return; arr[rear] = val; rear = (rear + 1) % arr.Length; count++; } public int Dequeue() { if (count == 0) return -1; int val = arr[front]; front = (front + 1) % arr.Length; count--; return val; } public string PrintQueue() { string result = ""; for (int i = 0; i < count; i++) { int index = (front + i) % arr.Length; result += arr[index] + " -> "; } return result + "null"; } } Queue q = new Queue(); q.Enqueue(10); q.Enqueue(20); q.Enqueue(30); q.Dequeue(); q.Enqueue(40); q.Enqueue(50); q.Dequeue(); q.Enqueue(60); Console.WriteLine(q.PrintQueue());
Attempts:
2 left
💡 Hint
Track the front and rear pointers carefully after each operation.
✗ Incorrect
After the first three enqueues, queue is 10 -> 20 -> 30 -> null. Dequeue removes 10, queue becomes 20 -> 30 -> null. Enqueue 40 and 50 adds them at rear: 20 -> 30 -> 40 -> 50 -> null. Dequeue removes 20, queue is 30 -> 40 -> 50 -> null. Enqueue 60 adds at rear: 30 -> 40 -> 50 -> 60 -> null.
🧠 Conceptual
intermediateWhat happens when the queue is full in array implementation?
In a queue implemented using a fixed-size array, what happens if you try to enqueue an element when the queue is already full?
Attempts:
2 left
💡 Hint
Think about fixed size arrays and their capacity limits.
✗ Incorrect
A fixed-size array cannot grow dynamically. When full, enqueue operations must be rejected or ignored to avoid overwriting data.
🔧 Debug
advancedIdentify the bug in this dequeue method
What error will occur when running this dequeue method in a queue implemented using an array?
DSA C#
public int Dequeue() { if (count == 0) return -1; int val = arr[front]; front = front + 1; count--; return val; }
Attempts:
2 left
💡 Hint
Consider what happens when front increments beyond the array size.
✗ Incorrect
The front index is incremented without wrapping around using modulo. When front exceeds array length, accessing arr[front] causes IndexOutOfRangeException.
❓ Predict Output
advancedOutput of circular queue after wrap-around enqueues and dequeues
Given a circular queue of size 4 implemented with an array, starting empty, perform these operations:
Enqueue 1, Enqueue 2, Enqueue 3, Dequeue, Enqueue 4, Dequeue, Enqueue 5.
What is the printed queue state?
Enqueue 1, Enqueue 2, Enqueue 3, Dequeue, Enqueue 4, Dequeue, Enqueue 5.
What is the printed queue state?
DSA C#
class CircularQueue { private int[] arr = new int[4]; private int front = 0; private int rear = 0; private int count = 0; public void Enqueue(int val) { if (count == arr.Length) return; arr[rear] = val; rear = (rear + 1) % arr.Length; count++; } public int Dequeue() { if (count == 0) return -1; int val = arr[front]; front = (front + 1) % arr.Length; count--; return val; } public string PrintQueue() { string result = ""; for (int i = 0; i < count; i++) { int index = (front + i) % arr.Length; result += arr[index] + " -> "; } return result + "null"; } } CircularQueue q = new CircularQueue(); q.Enqueue(1); q.Enqueue(2); q.Enqueue(3); q.Dequeue(); q.Enqueue(4); q.Dequeue(); q.Enqueue(5); Console.WriteLine(q.PrintQueue());
Attempts:
2 left
💡 Hint
Remember the circular wrap-around of front and rear pointers.
✗ Incorrect
After enqueuing 1,2,3 queue is 1->2->3->null. Dequeue removes 1, queue is 2->3->null. Enqueue 4 adds at rear: 2->3->4->null. Dequeue removes 2, queue is 3->4->null. Enqueue 5 adds at rear (wrap-around): 3->4->5->null.
🧠 Conceptual
expertWhy use circular queue instead of simple array queue?
Why is a circular queue implementation preferred over a simple array queue implementation when using a fixed-size array?
Attempts:
2 left
💡 Hint
Think about what happens to space when elements are dequeued in a simple array queue.
✗ Incorrect
In a simple array queue, dequeued elements leave empty space at the front that cannot be reused, causing inefficient space use. Circular queue uses modulo arithmetic to wrap around and reuse space.
