Challenge - 5 Problems
Queue Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Enqueue and Dequeue Operations
What is the printed state of the queue after the following operations?
Enqueue 10, Enqueue 20, Dequeue, Enqueue 30
Enqueue 10, Enqueue 20, Dequeue, Enqueue 30
DSA C
struct Node {
int data;
struct Node* next;
};
struct Queue {
struct Node *front, *rear;
};
void enqueue(struct Queue* q, int value) {
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = value;
temp->next = NULL;
if (q->rear == NULL) {
q->front = q->rear = temp;
return;
}
q->rear->next = temp;
q->rear = temp;
}
int dequeue(struct Queue* q) {
if (q->front == NULL) return -1;
struct Node* temp = q->front;
int val = temp->data;
q->front = q->front->next;
if (q->front == NULL) q->rear = NULL;
free(temp);
return val;
}
void printQueue(struct Queue* q) {
struct Node* temp = q->front;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("null\n");
}
int main() {
struct Queue q = {NULL, NULL};
enqueue(&q, 10);
enqueue(&q, 20);
dequeue(&q);
enqueue(&q, 30);
printQueue(&q);
return 0;
}Attempts:
2 left
💡 Hint
Remember that dequeue removes from the front of the queue.
✗ Incorrect
After enqueueing 10 and 20, the queue is 10 -> 20 -> null. Dequeue removes 10, so queue becomes 20 -> null. Enqueue 30 adds it to the rear, so queue is 20 -> 30 -> null.
❓ Predict Output
intermediate2:00remaining
Output after Multiple Dequeues
What is the output after these operations?
Enqueue 5, Enqueue 15, Enqueue 25, Dequeue, Dequeue, printQueue
Enqueue 5, Enqueue 15, Enqueue 25, Dequeue, Dequeue, printQueue
DSA C
struct Node {
int data;
struct Node* next;
};
struct Queue {
struct Node *front, *rear;
};
void enqueue(struct Queue* q, int value) {
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = value;
temp->next = NULL;
if (q->rear == NULL) {
q->front = q->rear = temp;
return;
}
q->rear->next = temp;
q->rear = temp;
}
int dequeue(struct Queue* q) {
if (q->front == NULL) return -1;
struct Node* temp = q->front;
int val = temp->data;
q->front = q->front->next;
if (q->front == NULL) q->rear = NULL;
free(temp);
return val;
}
void printQueue(struct Queue* q) {
struct Node* temp = q->front;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("null\n");
}
int main() {
struct Queue q = {NULL, NULL};
enqueue(&q, 5);
enqueue(&q, 15);
enqueue(&q, 25);
dequeue(&q);
dequeue(&q);
printQueue(&q);
return 0;
}Attempts:
2 left
💡 Hint
Two dequeues remove the first two elements.
✗ Incorrect
After enqueueing 5, 15, 25, the queue is 5 -> 15 -> 25 -> null. Two dequeues remove 5 and 15, leaving 25 -> null.
🔧 Debug
advanced2:00remaining
Identify the Bug in Dequeue Function
What error will occur when running this dequeue function if the queue is empty?
DSA C
int dequeue(struct Queue* q) { struct Node* temp = q->front; int val = temp->data; q->front = q->front->next; if (q->front == NULL) q->rear = NULL; free(temp); return val; }
Attempts:
2 left
💡 Hint
Check what happens if q->front is NULL before accessing temp->data.
✗ Incorrect
If the queue is empty, q->front is NULL. Dereferencing temp->data causes a segmentation fault (crash). The function lacks a check for empty queue.
❓ Predict Output
advanced2:00remaining
Output after Enqueue and Dequeue Sequence
What is the printed queue after these operations?
Enqueue 1, Enqueue 2, Enqueue 3, Dequeue, Enqueue 4, Dequeue, Enqueue 5, printQueue
Enqueue 1, Enqueue 2, Enqueue 3, Dequeue, Enqueue 4, Dequeue, Enqueue 5, printQueue
DSA C
struct Node {
int data;
struct Node* next;
};
struct Queue {
struct Node *front, *rear;
};
void enqueue(struct Queue* q, int value) {
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = value;
temp->next = NULL;
if (q->rear == NULL) {
q->front = q->rear = temp;
return;
}
q->rear->next = temp;
q->rear = temp;
}
int dequeue(struct Queue* q) {
if (q->front == NULL) return -1;
struct Node* temp = q->front;
int val = temp->data;
q->front = q->front->next;
if (q->front == NULL) q->rear = NULL;
free(temp);
return val;
}
void printQueue(struct Queue* q) {
struct Node* temp = q->front;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("null\n");
}
int main() {
struct Queue q = {NULL, NULL};
enqueue(&q, 1);
enqueue(&q, 2);
enqueue(&q, 3);
dequeue(&q);
enqueue(&q, 4);
dequeue(&q);
enqueue(&q, 5);
printQueue(&q);
return 0;
}Attempts:
2 left
💡 Hint
Each dequeue removes the front element.
✗ Incorrect
Queue after enqueue 1,2,3: 1->2->3->null. Dequeue removes 1: 2->3->null. Enqueue 4: 2->3->4->null. Dequeue removes 2: 3->4->null. Enqueue 5: 3->4->5->null.
🧠 Conceptual
expert2:00remaining
Why Use Linked List for Queue Implementation?
Which of the following is the best reason to implement a queue using a linked list instead of an array?
Attempts:
2 left
💡 Hint
Think about how arrays and linked lists handle size changes.
✗ Incorrect
Linked lists can grow or shrink dynamically without needing to allocate a fixed size or resize, unlike arrays which have fixed size or costly resizing.
