Challenge - 5 Problems
Tail Insert Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Tail Insert in Linked List
What is the printed linked list after inserting 10, 20, and 30 at the end using tail insert?
DSA C
typedef struct Node {
int data;
struct Node* next;
} Node;
#include <stdio.h>
#include <stdlib.h>
Node* tailInsert(Node* head, int val) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = val;
newNode->next = NULL;
if (head == NULL) {
return newNode;
}
Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
return head;
}
void printList(Node* head) {
Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
Node* head = NULL;
head = tailInsert(head, 10);
head = tailInsert(head, 20);
head = tailInsert(head, 30);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Remember that tail insert adds nodes at the end, preserving the order of insertion.
✗ Incorrect
The tailInsert function adds new nodes at the end of the list. After inserting 10, 20, and 30 in that order, the list prints as 10 -> 20 -> 30 -> NULL.
🧠 Conceptual
intermediate1:00remaining
Number of Nodes After Tail Insertions
If you start with an empty linked list and perform tail insertions of 5 nodes, how many nodes will the list contain?
Attempts:
2 left
💡 Hint
Each tail insertion adds exactly one node to the list.
✗ Incorrect
Starting from empty, each tail insertion adds one node. After 5 insertions, the list has 5 nodes.
🔧 Debug
advanced2:00remaining
Identify the Bug in Tail Insert Function
What error will occur when running this tail insert function code snippet?
DSA C
Node* tailInsert(Node* head, int val) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = val; newNode->next = NULL; Node* temp = head; if (head == NULL) { head = newNode; } while (temp->next != NULL) { temp = temp->next; } temp->next = newNode; return head; }
Attempts:
2 left
💡 Hint
Check what happens when head is NULL and you try to access temp->next.
✗ Incorrect
When head is NULL, the code sets head = newNode but continues to use temp = head and then temp->next. Since head was NULL before, temp->next access causes segmentation fault.
📝 Syntax
advanced1:30remaining
Correct Syntax for Tail Insert Loop
Which option correctly iterates to the last node in a singly linked list for tail insertion?
Attempts:
2 left
💡 Hint
You want to stop at the last node, not go past it.
✗ Incorrect
Option C correctly stops at the last node where next is NULL. Option C goes past the last node causing temp to be NULL. Option C is syntactically valid but missing braces and less clear. Option C is valid C but less explicit; however, since temp->next is a pointer, it works but option C is clearer and safer.
🚀 Application
expert3:00remaining
Final Linked List After Multiple Tail Inserts and One Deletion
Given an empty linked list, you perform tail insertions of values 1, 2, 3, 4, 5 in order. Then you delete the node with value 3. What is the printed linked list?
DSA C
typedef struct Node {
int data;
struct Node* next;
} Node;
#include <stdio.h>
#include <stdlib.h>
Node* tailInsert(Node* head, int val) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = val;
newNode->next = NULL;
if (head == NULL) {
return newNode;
}
Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
return head;
}
Node* deleteNode(Node* head, int val) {
if (head == NULL) return NULL;
if (head->data == val) {
Node* temp = head->next;
free(head);
return temp;
}
Node* curr = head;
while (curr->next != NULL && curr->next->data != val) {
curr = curr->next;
}
if (curr->next != NULL) {
Node* temp = curr->next;
curr->next = temp->next;
free(temp);
}
return head;
}
void printList(Node* head) {
Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
Node* head = NULL;
head = tailInsert(head, 1);
head = tailInsert(head, 2);
head = tailInsert(head, 3);
head = tailInsert(head, 4);
head = tailInsert(head, 5);
head = deleteNode(head, 3);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Deleting node with value 3 removes it from the list, others remain in order.
✗ Incorrect
After inserting 1 to 5, the list is 1 -> 2 -> 3 -> 4 -> 5 -> NULL. Deleting node with value 3 removes it, resulting in 1 -> 2 -> 4 -> 5 -> NULL.
