Challenge - 5 Problems
Doubly Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output after inserting 10 at the beginning?
Consider a doubly linked list initially containing nodes with values 20 -> 30 -> 40 -> null. We insert a new node with value 10 at the beginning. What is the printed list after insertion?
DSA C
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
void printList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("null\n");
}
struct Node* insertAtBeginning(struct Node* head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = head;
if (head != NULL) {
head->prev = newNode;
}
head = newNode;
return head;
}
int main() {
struct Node* head = (struct Node*)malloc(sizeof(struct Node));
struct Node* second = (struct Node*)malloc(sizeof(struct Node));
struct Node* third = (struct Node*)malloc(sizeof(struct Node));
head->data = 20; head->prev = NULL; head->next = second;
second->data = 30; second->prev = head; second->next = third;
third->data = 40; third->prev = second; third->next = NULL;
head = insertAtBeginning(head, 10);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Remember to update the previous pointer of the old head node.
✗ Incorrect
When inserting at the beginning, the new node becomes the head. Its next points to the old head, and the old head's prev points back to the new node. The list order becomes 10 -> 20 -> 30 -> 40 -> null.
❓ Predict Output
intermediate2:00remaining
What is the output after inserting 5 at the beginning of an empty list?
Given an empty doubly linked list (head is NULL), we insert a node with value 5 at the beginning. What is the printed list after insertion?
DSA C
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
void printList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("null\n");
}
struct Node* insertAtBeginning(struct Node* head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = head;
if (head != NULL) {
head->prev = newNode;
}
head = newNode;
return head;
}
int main() {
struct Node* head = NULL;
head = insertAtBeginning(head, 5);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Inserting into an empty list creates a single node that is both head and tail.
✗ Incorrect
Since the list was empty, the new node with value 5 becomes the only node. The list prints as 5 -> null.
🔧 Debug
advanced2:00remaining
What error occurs when inserting at beginning with missing prev update?
Consider this code snippet for inserting at the beginning of a doubly linked list:
struct Node* insertAtBeginning(struct Node* head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = head;
// Missing: head->prev = newNode;
head = newNode;
return head;
}
What error or problem will occur when printing the list after insertion?
Attempts:
2 left
💡 Hint
Think about how the prev pointers help in traversing backward and maintaining list integrity.
✗ Incorrect
If the old head's prev pointer is not updated to the new node, backward links are broken. This can cause infinite loops or incorrect traversal when printing or traversing backward.
🧠 Conceptual
advanced2:00remaining
Why is updating the prev pointer necessary when inserting at the beginning?
In a doubly linked list, when inserting a new node at the beginning, why must we update the previous pointer of the old head node to point to the new node?
Attempts:
2 left
💡 Hint
Think about how doubly linked lists allow traversal in both directions.
✗ Incorrect
The prev pointer of the old head must point to the new node to maintain correct backward traversal from tail to head.
❓ Predict Output
expert3:00remaining
What is the output after multiple insertions at beginning?
Starting with an empty doubly linked list, we insert nodes with values 50, then 40, then 30, then 20, then 10 at the beginning in that order. What is the printed list after all insertions?
DSA C
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
void printList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("null\n");
}
struct Node* insertAtBeginning(struct Node* head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = head;
if (head != NULL) {
head->prev = newNode;
}
head = newNode;
return head;
}
int main() {
struct Node* head = NULL;
head = insertAtBeginning(head, 50);
head = insertAtBeginning(head, 40);
head = insertAtBeginning(head, 30);
head = insertAtBeginning(head, 20);
head = insertAtBeginning(head, 10);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Each new node becomes the new head, pushing existing nodes to the right.
✗ Incorrect
Inserting at the beginning reverses the order of insertion. The last inserted node (10) becomes the head, followed by 20, 30, 40, and 50.
