Challenge - 5 Problems
Linked List Queue Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of enqueue operations on linked list queue
What is the printed state of the queue after enqueuing 10, 20, and 30 in order?
DSA C
typedef struct Node {
int data;
struct Node* next;
} Node;
typedef struct Queue {
Node* front;
Node* rear;
} Queue;
#include <stdio.h>
#include <stdlib.h>
void enqueue(Queue* q, int value) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = value;
newNode->next = NULL;
if (q->rear == NULL) {
q->front = q->rear = newNode;
return;
}
q->rear->next = newNode;
q->rear = newNode;
}
void printQueue(Queue* q) {
Node* temp = q->front;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("null\n");
}
int main() {
Queue q = {NULL, NULL};
enqueue(&q, 10);
enqueue(&q, 20);
enqueue(&q, 30);
printQueue(&q);
return 0;
}Attempts:
2 left
💡 Hint
Remember that enqueue adds elements at the rear of the queue.
✗ Incorrect
Enqueue adds elements at the rear. So the order is the same as insertion: 10, then 20, then 30.
❓ Predict Output
intermediate2:00remaining
Output after enqueue and one dequeue operation
What is the printed state of the queue after enqueuing 5, 15, 25 and then dequeuing one element?
DSA C
typedef struct Node {
int data;
struct Node* next;
} Node;
typedef struct Queue {
Node* front;
Node* rear;
} Queue;
#include <stdio.h>
#include <stdlib.h>
void enqueue(Queue* q, int value) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = value;
newNode->next = NULL;
if (q->rear == NULL) {
q->front = q->rear = newNode;
return;
}
q->rear->next = newNode;
q->rear = newNode;
}
int dequeue(Queue* q) {
if (q->front == NULL) return -1;
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(Queue* q) {
Node* temp = q->front;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("null\n");
}
int main() {
Queue q = {NULL, NULL};
enqueue(&q, 5);
enqueue(&q, 15);
enqueue(&q, 25);
dequeue(&q);
printQueue(&q);
return 0;
}Attempts:
2 left
💡 Hint
Dequeue removes from the front of the queue.
✗ Incorrect
After removing the first element (5), the queue has 15 and 25 in order.
🔧 Debug
advanced2:00remaining
Identify the bug in enqueue function
Which option shows the correct fix to avoid memory leak in this enqueue function?
DSA C
void enqueue(Queue* q, int value) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = value; newNode->next = NULL; if (q->rear == NULL) { q->front = newNode; // Missing assignment to q->rear here return; } q->rear->next = newNode; q->rear = newNode; }
Attempts:
2 left
💡 Hint
Both front and rear must point to the new node when queue is empty.
✗ Incorrect
If rear is not set, it remains NULL causing issues and potential memory leaks.
🧠 Conceptual
advanced1:30remaining
Why use linked list for queue enqueue?
Why is a linked list preferred over an array for implementing enqueue in a queue?
Attempts:
2 left
💡 Hint
Think about resizing and shifting in arrays.
✗ Incorrect
Linked lists grow dynamically and enqueue at rear in constant time without shifting elements.
❓ Predict Output
expert3:00remaining
Output after multiple enqueue and dequeue operations
What is the printed state of the queue after these operations?
enqueue 1
enqueue 2
enqueue 3
dequeue
enqueue 4
dequeue
enqueue 5
printQueue
DSA C
typedef struct Node {
int data;
struct Node* next;
} Node;
typedef struct Queue {
Node* front;
Node* rear;
} Queue;
#include <stdio.h>
#include <stdlib.h>
void enqueue(Queue* q, int value) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = value;
newNode->next = NULL;
if (q->rear == NULL) {
q->front = q->rear = newNode;
return;
}
q->rear->next = newNode;
q->rear = newNode;
}
int dequeue(Queue* q) {
if (q->front == NULL) return -1;
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(Queue* q) {
Node* temp = q->front;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("null\n");
}
int main() {
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
Track the queue after each operation carefully.
✗ Incorrect
After dequeuing twice, elements 1 and 2 are removed. Remaining are 3, then enqueue 4 and 5 at rear.
