Challenge - 5 Problems
Enqueue Mastery
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 circular queue after these enqueue operations?
DSA Java
class CircularQueue { int[] arr; int front, rear, size, capacity; CircularQueue(int capacity) { this.capacity = capacity; arr = new int[capacity]; front = 0; rear = -1; size = 0; } void enqueue(int x) { if (size == capacity) return; rear = (rear + 1) % capacity; arr[rear] = x; size++; } void printQueue() { for (int i = 0; i < size; i++) { System.out.print(arr[(front + i) % capacity] + " -> "); } System.out.println("null"); } } public class Main { public static void main(String[] args) { CircularQueue q = new CircularQueue(4); q.enqueue(10); q.enqueue(20); q.enqueue(30); q.enqueue(40); q.printQueue(); } }
Attempts:
2 left
💡 Hint
Remember that enqueue adds elements at the rear and the queue prints from front to rear.
✗ Incorrect
The enqueue method adds elements in order at the rear index, wrapping around if needed. Since the queue capacity is 4 and we add 4 elements, all are stored in order. The printQueue method prints from front to rear, so output is 10 -> 20 -> 30 -> 40 -> null.
❓ Predict Output
intermediateResult of Enqueue When Queue is Full
What will be the output after trying to enqueue an element into a full queue?
DSA Java
class Queue { int[] arr; int front, rear, size, capacity; Queue(int capacity) { this.capacity = capacity; arr = new int[capacity]; front = 0; rear = -1; size = 0; } void enqueue(int x) { if (size == capacity) { System.out.println("Queue is full"); return; } rear++; arr[rear] = x; size++; } void printQueue() { for (int i = front; i <= rear; i++) { System.out.print(arr[i] + " -> "); } System.out.println("null"); } } public class Main { public static void main(String[] args) { Queue q = new Queue(3); q.enqueue(1); q.enqueue(2); q.enqueue(3); q.enqueue(4); q.printQueue(); } }
Attempts:
2 left
💡 Hint
Check the condition that prevents adding when the queue is full.
✗ Incorrect
The queue capacity is 3. After adding 3 elements, the queue is full. The fourth enqueue prints "Queue is full" and does not add the element. The printQueue method prints the existing 3 elements.
🔧 Debug
advancedIdentify the Bug in Enqueue Method
What error will occur when running this enqueue method?
DSA Java
class Queue { int[] arr = new int[3]; int rear = 0; int size = 0; int capacity = 3; void enqueue(int x) { if (size == capacity) { System.out.println("Full"); return; } arr[rear] = x; rear++; size++; } } public class Main { public static void main(String[] args) { Queue q = new Queue(); q.enqueue(1); q.enqueue(2); q.enqueue(3); q.enqueue(4); } }
Attempts:
2 left
💡 Hint
Check how rear index changes and array size.
✗ Incorrect
No error occurs. The `if (size == capacity)` check prevents overflow. After three enqueues, `size` is 3 and `rear` is 3. The fourth enqueue prints "Full" and returns before assigning to `arr[rear]`, avoiding ArrayIndexOutOfBoundsException.
❓ Predict Output
advancedOutput After Multiple Enqueues and Wrap Around
What is the printed queue after these operations on a circular queue of capacity 5?
DSA Java
class CircularQueue { int[] arr; int front, rear, size, capacity; CircularQueue(int capacity) { this.capacity = capacity; arr = new int[capacity]; front = 0; rear = -1; size = 0; } void enqueue(int x) { if (size == capacity) return; rear = (rear + 1) % capacity; arr[rear] = x; size++; } void dequeue() { if (size == 0) return; front = (front + 1) % capacity; size--; } void printQueue() { for (int i = 0; i < size; i++) { System.out.print(arr[(front + i) % capacity] + " -> "); } System.out.println("null"); } } public class Main { public static void main(String[] args) { CircularQueue q = new CircularQueue(5); q.enqueue(1); q.enqueue(2); q.enqueue(3); q.enqueue(4); q.enqueue(5); q.dequeue(); q.dequeue(); q.enqueue(6); q.enqueue(7); q.printQueue(); } }
Attempts:
2 left
💡 Hint
Remember dequeue moves front forward and enqueue wraps rear index.
✗ Incorrect
After enqueuing 1 to 5, queue is full. Dequeue twice removes 1 and 2, so front moves forward. Enqueue 6 and 7 wrap rear index to start. The queue now contains 3,4,5,6,7 in order.
🧠 Conceptual
expertMaximum Number of Enqueue Operations Before Full
Given a queue implemented with an array of capacity N, starting empty, how many enqueue operations can be performed before the queue is full?
Attempts:
2 left
💡 Hint
Consider that the queue size cannot exceed its capacity.
✗ Incorrect
A queue with capacity N can hold exactly N elements. Starting empty, you can enqueue N times before it becomes full.
