Challenge - 5 Problems
Doubly Linked List Insertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output after inserting at position 3
What is the printed state of the doubly linked list after inserting the value 25 at position 3?
DSA C
typedef struct Node {
int data;
struct Node* prev;
struct Node* next;
} Node;
#include <stdio.h>
#include <stdlib.h>
void printList(Node* head) {
Node* temp = head;
while (temp != NULL) {
printf("%d <-> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
Node* insertAtPosition(Node* head, int data, int pos) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
if (pos == 1) {
newNode->next = head;
if (head != NULL) head->prev = newNode;
return newNode;
}
Node* temp = head;
int count = 1;
while (temp != NULL && count < pos - 1) {
temp = temp->next;
count++;
}
if (temp == NULL) return head; // position out of bounds
newNode->next = temp->next;
if (temp->next != NULL) temp->next->prev = newNode;
temp->next = newNode;
newNode->prev = temp;
return head;
}
int main() {
Node* head = NULL;
head = insertAtPosition(head, 10, 1);
head = insertAtPosition(head, 20, 2);
head = insertAtPosition(head, 30, 3);
head = insertAtPosition(head, 25, 3);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Remember that position 3 means the new node goes after the second node.
✗ Incorrect
The new node with value 25 is inserted after the second node (20), so the order becomes 10, 20, 25, 30.
❓ Predict Output
intermediate2:00remaining
Output after inserting at position 1 (head)
What is the printed state of the doubly linked list after inserting the value 5 at position 1?
DSA C
typedef struct Node {
int data;
struct Node* prev;
struct Node* next;
} Node;
#include <stdio.h>
#include <stdlib.h>
void printList(Node* head) {
Node* temp = head;
while (temp != NULL) {
printf("%d <-> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
Node* insertAtPosition(Node* head, int data, int pos) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
if (pos == 1) {
newNode->next = head;
if (head != NULL) head->prev = newNode;
return newNode;
}
Node* temp = head;
int count = 1;
while (temp != NULL && count < pos - 1) {
temp = temp->next;
count++;
}
if (temp == NULL) return head; // position out of bounds
newNode->next = temp->next;
if (temp->next != NULL) temp->next->prev = newNode;
temp->next = newNode;
newNode->prev = temp;
return head;
}
int main() {
Node* head = NULL;
head = insertAtPosition(head, 10, 1);
head = insertAtPosition(head, 20, 2);
head = insertAtPosition(head, 30, 3);
head = insertAtPosition(head, 5, 1);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Inserting at position 1 means the new node becomes the new head.
✗ Incorrect
The new node with value 5 is inserted at the start, so the list starts with 5 followed by the original nodes.
🔧 Debug
advanced2:30remaining
Identify the bug causing incorrect insertion at tail
The following code attempts to insert a node at a specific position in a doubly linked list. However, inserting at the last position does not link the new node correctly. What is the bug?
DSA C
Node* insertAtPosition(Node* head, int data, int pos) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; newNode->prev = NULL; newNode->next = NULL; if (pos == 1) { newNode->next = head; if (head != NULL) head->prev = newNode; return newNode; } Node* temp = head; int count = 1; while (temp != NULL && count < pos - 1) { temp = temp->next; count++; } if (temp == NULL) return head; // position out of bounds newNode->next = temp->next; if (temp->next != NULL) temp->next->prev = newNode; temp->next = newNode; newNode->prev = temp; return head; }
Attempts:
2 left
💡 Hint
Check what happens when inserting at the end of the list.
✗ Incorrect
When inserting at the last position (pos = length + 1), temp points to the last node, temp->next is NULL. The code sets newNode->next = NULL and temp->next = newNode, which is correct. But if pos is beyond length + 1, temp becomes NULL and insertion is skipped. The bug is that the code does not allow insertion exactly at the tail if pos is length + 1 because the while loop stops early or temp becomes NULL incorrectly.
❓ Predict Output
advanced2:00remaining
Output after inserting at position beyond list length
What is the printed state of the doubly linked list after attempting to insert the value 40 at position 10, given the list has only 3 nodes?
DSA C
typedef struct Node {
int data;
struct Node* prev;
struct Node* next;
} Node;
#include <stdio.h>
#include <stdlib.h>
void printList(Node* head) {
Node* temp = head;
while (temp != NULL) {
printf("%d <-> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
Node* insertAtPosition(Node* head, int data, int pos) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
if (pos == 1) {
newNode->next = head;
if (head != NULL) head->prev = newNode;
return newNode;
}
Node* temp = head;
int count = 1;
while (temp != NULL && count < pos - 1) {
temp = temp->next;
count++;
}
if (temp == NULL) return head; // position out of bounds
newNode->next = temp->next;
if (temp->next != NULL) temp->next->prev = newNode;
temp->next = newNode;
newNode->prev = temp;
return head;
}
int main() {
Node* head = NULL;
head = insertAtPosition(head, 10, 1);
head = insertAtPosition(head, 20, 2);
head = insertAtPosition(head, 30, 3);
head = insertAtPosition(head, 40, 10);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Inserting beyond the list length should not change the list.
✗ Incorrect
Since position 10 is beyond the current list length (3), the insertion is ignored and the list remains unchanged.
🧠 Conceptual
expert1:30remaining
Time complexity of insertion at specific position
What is the time complexity of inserting a node at a specific position in a doubly linked list of length n, assuming you have only the head pointer?
Attempts:
2 left
💡 Hint
Think about how you find the position before inserting.
✗ Incorrect
Without direct access, you must traverse from the head to the position before insertion, which takes O(n) time.
