Challenge - 5 Problems
Dequeue Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Dequeue Front Operation
What is the output of the following C code that performs a dequeue front operation on a dequeue?
DSA C
#include <stdio.h> #include <stdlib.h> #define MAX 5 int dequeue[MAX]; int front = -1, rear = -1; void enqueueRear(int x) { if ((front == 0 && rear == MAX - 1) || (rear == (front - 1 + MAX) % MAX)) { printf("Queue is Full\n"); return; } else if (front == -1) { front = rear = 0; dequeue[rear] = x; } else if (rear == MAX - 1 && front != 0) { rear = 0; dequeue[rear] = x; } else { rear++; dequeue[rear] = x; } } int dequeueFront() { if (front == -1) { printf("Queue is Empty\n"); return -1; } int data = dequeue[front]; if (front == rear) { front = rear = -1; } else if (front == MAX - 1) { front = 0; } else { front++; } return data; } int main() { enqueueRear(10); enqueueRear(20); enqueueRear(30); int val = dequeueFront(); printf("Dequeued: %d\n", val); printf("Front element now: %d\n", dequeue[front]); return 0; }
Attempts:
2 left
💡 Hint
Remember that dequeueFront removes the element at the front and updates the front pointer.
✗ Incorrect
The dequeueFront function removes the element at the front index, which is 10, then moves front to the next element, which is 20. So the dequeued value is 10 and the new front element is 20.
❓ Predict Output
intermediate2:00remaining
Output after Dequeue Rear Operation
What is the output of this C code after performing a dequeue rear operation on a dequeue?
DSA C
#include <stdio.h> #include <stdlib.h> #define MAX 4 int dequeue[MAX]; int front = 0, rear = 2; void enqueueRear(int x) { if ((front == 0 && rear == MAX - 1) || (rear == (front - 1 + MAX) % MAX)) { printf("Queue is Full\n"); return; } if (rear == MAX - 1 && front != 0) { rear = 0; } else { rear++; } dequeue[rear] = x; } int dequeueRear() { if (front == -1) { printf("Queue is Empty\n"); return -1; } int data = dequeue[rear]; if (front == rear) { front = rear = -1; } else if (rear == 0) { rear = MAX - 1; } else { rear--; } return data; } int main() { dequeue[0] = 5; dequeue[1] = 10; dequeue[2] = 15; int val = dequeueRear(); printf("Dequeued from rear: %d\n", val); printf("Rear element now: %d\n", dequeue[rear]); return 0; }
Attempts:
2 left
💡 Hint
Dequeue rear removes the element at the rear index and updates rear pointer accordingly.
✗ Incorrect
The dequeueRear function removes the element at rear index 2, which is 15, then moves rear pointer back to 1, so the new rear element is 10.
🔧 Debug
advanced2:00remaining
Identify the Bug in Dequeue Front Function
What error will occur when running this dequeue front function code snippet?
DSA C
int dequeueFront() { if (front == -1) { printf("Queue is Empty\n"); return -1; } int data = dequeue[front]; if (front == rear) { front = rear = -1; } else if (front == MAX - 1) { front = 0; } else { front++; } return data; }
Attempts:
2 left
💡 Hint
Check the condition that resets front to 0 after reaching the end.
✗ Incorrect
The condition should check if front == MAX - 1, not front == MAX. When front == MAX, it accesses dequeue[MAX], which is out of bounds causing undefined behavior.
🧠 Conceptual
advanced2:00remaining
Understanding Circular Dequeue Full Condition
Which condition correctly checks if a circular dequeue is full?
Attempts:
2 left
💡 Hint
In a circular dequeue, full means rear is just before front in a circular manner.
✗ Incorrect
Option A correctly checks if rear is just before front either at the end or wrapped around, indicating the dequeue is full.
🚀 Application
expert3:00remaining
Resulting Dequeue State After Operations
Given an empty dequeue of size 5, after performing these operations in order:
1. enqueueRear(1)
2. enqueueRear(2)
3. enqueueFront(3)
4. dequeueRear()
5. enqueueFront(4)
What is the state of the dequeue from front to rear?
Attempts:
2 left
💡 Hint
Track front and rear pointers carefully after each operation.
✗ Incorrect
After enqueueRear(1) and enqueueRear(2), dequeue is 1 -> 2.
enqueueFront(3) adds 3 at front: 3 -> 1 -> 2.
dequeueRear() removes 2: 3 -> 1.
enqueueFront(4) adds 4 at front: 4 -> 3 -> 1.
So final state is 4 -> 3 -> 1 -> null.
