Challenge - 5 Problems
Head Insert 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 nodes at the beginning?
Consider a singly linked list initially empty. We insert nodes with values 10, 20, and 30 at the beginning one by one. What is the final state of the linked list?
DSA C
struct Node {
int data;
struct Node* next;
};
struct Node* head = NULL;
void insertAtBeginning(int val) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = val;
newNode->next = head;
head = newNode;
}
int main() {
insertAtBeginning(10);
insertAtBeginning(20);
insertAtBeginning(30);
struct Node* temp = head;
while(temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
return 0;
}Attempts:
2 left
💡 Hint
Remember, inserting at the beginning means the newest node becomes the head.
✗ Incorrect
Each new node is added before the current head, so the list order reverses the insertion order.
❓ Predict Output
intermediate2:00remaining
What happens if we insert a node with value 0 at the beginning of a list with 5 -> 15 -> NULL?
Given a linked list with nodes 5 -> 15 -> NULL, we insert a node with value 0 at the beginning. What is the new list?
DSA C
struct Node {
int data;
struct Node* next;
};
struct Node* head = NULL;
void insertAtBeginning(int val) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = val;
newNode->next = head;
head = newNode;
}
int main() {
struct Node* first = (struct Node*)malloc(sizeof(struct Node));
struct Node* second = (struct Node*)malloc(sizeof(struct Node));
first->data = 5;
first->next = second;
second->data = 15;
second->next = NULL;
head = first;
insertAtBeginning(0);
struct Node* temp = head;
while(temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
return 0;
}Attempts:
2 left
💡 Hint
Inserting at the beginning changes the head to the new node.
✗ Incorrect
The new node with value 0 becomes the head, so the list starts with 0.
🔧 Debug
advanced2:00remaining
Why does this head insert code cause a segmentation fault?
Identify the bug in the following code that causes a segmentation fault when inserting at the beginning of a linked list.
DSA C
struct Node {
int data;
struct Node* next;
};
struct Node* head = NULL;
void insertAtBeginning(int val) {
struct Node* newNode;
newNode->data = val;
newNode->next = head;
head = newNode;
}
int main() {
insertAtBeginning(10);
return 0;
}Attempts:
2 left
💡 Hint
Check if newNode points to valid memory before accessing its fields.
✗ Incorrect
newNode is declared but not assigned memory with malloc, so accessing newNode->data causes a crash.
❓ Predict Output
advanced2:00remaining
What is the output after multiple head insertions with this code?
Given the code below, what is printed after inserting 1, 2, 3 at the beginning in that order?
DSA C
struct Node {
int data;
struct Node* next;
};
struct Node* head = NULL;
void insertAtBeginning(int val) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = val;
newNode->next = head;
head = newNode;
}
int main() {
insertAtBeginning(1);
insertAtBeginning(2);
insertAtBeginning(3);
struct Node* temp = head;
while(temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
return 0;
}Attempts:
2 left
💡 Hint
The last inserted node becomes the head.
✗ Incorrect
Each new node is inserted at the front, so the list order is reversed from insertion order.
🧠 Conceptual
expert1:00remaining
What is the time complexity of inserting a node at the beginning of a singly linked list?
Choose the correct time complexity for inserting a new node at the beginning (head) of a singly linked list.
Attempts:
1 left
💡 Hint
Inserting at the head does not require traversal of the list.
✗ Incorrect
Insertion at the beginning only changes a few pointers and does not depend on list size, so it is constant time.
