Challenge - 5 Problems
Queue Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of enqueue operation on a queue
What is the printed state of the queue after performing the enqueue operations in the given code?
DSA C++
class Queue { public: int arr[5]; int front, rear; Queue() { front = -1; rear = -1; } void enqueue(int x) { if (rear == 4) { return; // Queue full } if (front == -1) front = 0; rear++; arr[rear] = x; } void printQueue() { for (int i = front; i <= rear; i++) { std::cout << arr[i] << " -> "; } std::cout << "null" << std::endl; } }; int main() { Queue q; q.enqueue(10); q.enqueue(20); q.enqueue(30); q.printQueue(); return 0; }
Attempts:
2 left
💡 Hint
Trace the enqueue steps and how front and rear pointers change.
✗ Incorrect
The queue starts empty with front and rear at -1. Enqueue 10 sets front=0, rear=0, arr[0]=10. Then enqueue 20 increments rear to 1, arr[1]=20. Enqueue 30 increments rear to 2, arr[2]=30. Printing from front=0 to rear=2 outputs 10 -> 20 -> 30 -> null.
❓ Predict Output
intermediateEnqueue operation on a circular queue
What is the output after enqueueing elements in the circular queue code below?
DSA C++
class CircularQueue { public: int arr[5]; int front, rear; CircularQueue() { front = -1; rear = -1; } void enqueue(int x) { if ((rear + 1) % 5 == front) { return; // Queue full } if (front == -1) front = 0; rear = (rear + 1) % 5; arr[rear] = x; } void printQueue() { if (front == -1) { std::cout << "null" << std::endl; return; } int i = front; while (true) { std::cout << arr[i] << " -> "; if (i == rear) break; i = (i + 1) % 5; } std::cout << "null" << std::endl; } }; int main() { CircularQueue q; q.enqueue(1); q.enqueue(2); q.enqueue(3); q.enqueue(4); q.enqueue(5); // This enqueue will be ignored (queue full) q.printQueue(); return 0; }
Attempts:
2 left
💡 Hint
Remember the circular queue stops enqueue when full.
✗ Incorrect
The circular queue has size 5 but can hold only 4 elements before full. The fifth enqueue is ignored. So only 1,2,3,4 are enqueued and printed.
🔧 Debug
advancedIdentify the error in enqueue method
What error will the following enqueue method cause when called on an empty queue?
DSA C++
void enqueue(int x) { if (rear == 4) { std::cout << "Queue full" << std::endl; return; } rear++; arr[rear] = x; if (front == -1) front = 0; }
Attempts:
2 left
💡 Hint
Check the order of updating rear and front pointers.
✗ Incorrect
rear is incremented before front is checked. If front was -1, it should be set to 0 before rear increments. Here front stays -1 causing logical errors in queue state.
🧠 Conceptual
advancedMaximum elements in a circular queue of size N
For a circular queue implemented with an array of size N, what is the maximum number of elements it can hold?
Attempts:
2 left
💡 Hint
Consider how front and rear pointers are used to detect full condition.
✗ Incorrect
Circular queue uses one slot empty to differentiate full and empty states, so max elements is N-1.
❓ Predict Output
expertOutput after multiple enqueue and dequeue operations
What is the printed state of the queue after the following operations?
DSA C++
class Queue { public: int arr[5]; int front, rear; Queue() { front = -1; rear = -1; } void enqueue(int x) { if (rear == 4) return; if (front == -1) front = 0; rear++; arr[rear] = x; } void dequeue() { if (front == -1 || front > rear) { return; } front++; if (front > rear) { front = -1; rear = -1; } } void printQueue() { if (front == -1) { std::cout << "null" << std::endl; return; } for (int i = front; i <= rear; i++) { std::cout << arr[i] << " -> "; } std::cout << "null" << std::endl; } }; int main() { Queue q; q.enqueue(5); q.enqueue(10); q.enqueue(15); q.dequeue(); q.enqueue(20); q.dequeue(); q.enqueue(25); q.printQueue(); return 0; }
Attempts:
2 left
💡 Hint
Track front and rear after each operation carefully.
✗ Incorrect
After enqueue 5,10,15: front=0, rear=2, queue=[5,10,15]. Dequeue moves front to 1 (10). Enqueue 20 at rear=3. Dequeue moves front to 2 (15). Enqueue 25 at rear=4. Final queue from front=2 to rear=4 is 15 -> 20 -> 25 -> null.
