Challenge - 5 Problems
Tail Insert Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Tail Insert in a Singly Linked List
What is the printed state of the linked list after inserting 10, 20, and 30 at the end using tail insertion?
DSA C++
#include <iostream> struct Node { int data; Node* next; Node(int val) : data(val), next(nullptr) {} }; class LinkedList { public: Node* head; LinkedList() : head(nullptr) {} void insertAtEnd(int val) { Node* newNode = new Node(val); if (!head) { head = newNode; return; } Node* temp = head; while (temp->next) { temp = temp->next; } temp->next = newNode; } void printList() { Node* temp = head; while (temp) { std::cout << temp->data << " -> "; temp = temp->next; } std::cout << "null" << std::endl; } }; int main() { LinkedList list; list.insertAtEnd(10); list.insertAtEnd(20); list.insertAtEnd(30); list.printList(); return 0; }
Attempts:
2 left
💡 Hint
Think about how tail insertion adds nodes at the end, preserving order.
✗ Incorrect
Tail insertion adds each new node at the end of the list, so the order of insertion is preserved in the list.
❓ Predict Output
intermediateResulting List After Multiple Tail Inserts
What is the linked list state after inserting 5, then 15, then 25 at the end?
DSA C++
#include <iostream> struct Node { int data; Node* next; Node(int val) : data(val), next(nullptr) {} }; class LinkedList { public: Node* head; LinkedList() : head(nullptr) {} void insertAtEnd(int val) { Node* newNode = new Node(val); if (!head) { head = newNode; return; } Node* temp = head; while (temp->next) { temp = temp->next; } temp->next = newNode; } void printList() { Node* temp = head; while (temp) { std::cout << temp->data << " -> "; temp = temp->next; } std::cout << "null" << std::endl; } }; int main() { LinkedList list; list.insertAtEnd(5); list.insertAtEnd(15); list.insertAtEnd(25); list.printList(); return 0; }
Attempts:
2 left
💡 Hint
Tail insertion keeps the order of insertion intact.
✗ Incorrect
Each new node is added at the end, so the list order matches insertion order.
🔧 Debug
advancedIdentify the Bug in Tail Insert Function
What error will occur when running this tail insert code snippet?
DSA C++
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
class LinkedList {
public:
Node* head;
LinkedList() : head(nullptr) {}
void insertAtEnd(int val) {
Node* newNode = new Node(val);
if (head == nullptr) {
head = newNode;
return;
}
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
}
};Attempts:
2 left
💡 Hint
Check what happens when the list is empty and you assign head = newNode but continue to use temp.
✗ Incorrect
When head is nullptr, head is set to newNode but the code continues to traverse from head, which now points to newNode. Then temp->next is assigned newNode again, creating a cycle causing infinite loop or crash.
❓ Predict Output
advancedOutput After Tail Insert with Multiple Nodes
What is the output after inserting nodes with values 1, 2, 3, 4, 5 at the end of an empty list?
DSA C++
#include <iostream> struct Node { int data; Node* next; Node(int val) : data(val), next(nullptr) {} }; class LinkedList { public: Node* head; LinkedList() : head(nullptr) {} void insertAtEnd(int val) { Node* newNode = new Node(val); if (!head) { head = newNode; return; } Node* temp = head; while (temp->next) { temp = temp->next; } temp->next = newNode; } void printList() { Node* temp = head; while (temp) { std::cout << temp->data << " -> "; temp = temp->next; } std::cout << "null" << std::endl; } }; int main() { LinkedList list; list.insertAtEnd(1); list.insertAtEnd(2); list.insertAtEnd(3); list.insertAtEnd(4); list.insertAtEnd(5); list.printList(); return 0; }
Attempts:
2 left
💡 Hint
Tail insertion preserves the order of insertion.
✗ Incorrect
Nodes are added at the end, so the list shows values in the order inserted.
🧠 Conceptual
expertTime Complexity of Tail Insert in Singly Linked List Without Tail Pointer
What is the time complexity of inserting a node at the end of a singly linked list when you do NOT maintain a tail pointer?
Attempts:
2 left
💡 Hint
Think about how many nodes you must visit to find the end each time.
✗ Incorrect
Without a tail pointer, you must traverse the entire list to find the last node before inserting, which takes linear time proportional to the list size.
