Challenge - 5 Problems
Insert at Middle Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after inserting at position 3 in a singly linked list
What is the printed state of the linked list after inserting the value 99 at position 3?
DSA C++
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
void insertAtPosition(Node*& head, int val, int pos) {
Node* newNode = new Node(val);
if (pos == 1) {
newNode->next = head;
head = newNode;
return;
}
Node* temp = head;
for (int i = 1; i < pos - 1 && temp != nullptr; i++) {
temp = temp->next;
}
if (temp == nullptr) return; // position out of bounds
newNode->next = temp->next;
temp->next = newNode;
}
void printList(Node* head) {
while (head) {
std::cout << head->data << " -> ";
head = head->next;
}
std::cout << "null" << std::endl;
}
int main() {
Node* head = new Node(10);
head->next = new Node(20);
head->next->next = new Node(30);
head->next->next->next = new Node(40);
insertAtPosition(head, 99, 3);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Remember that position 3 means the new node should be the third node in the list.
✗ Incorrect
The function inserts the new node at position 3, so the list becomes 10 -> 20 -> 99 -> 30 -> 40 -> null.
❓ Predict Output
intermediateResult of inserting at position 1 (head) in a singly linked list
What is the printed state of the linked list after inserting the value 5 at position 1?
DSA C++
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
void insertAtPosition(Node*& head, int val, int pos) {
Node* newNode = new Node(val);
if (pos == 1) {
newNode->next = head;
head = newNode;
return;
}
Node* temp = head;
for (int i = 1; i < pos - 1 && temp != nullptr; i++) {
temp = temp->next;
}
if (temp == nullptr) return; // position out of bounds
newNode->next = temp->next;
temp->next = newNode;
}
void printList(Node* head) {
while (head) {
std::cout << head->data << " -> ";
head = head->next;
}
std::cout << "null" << std::endl;
}
int main() {
Node* head = new Node(10);
head->next = new Node(20);
head->next->next = new Node(30);
insertAtPosition(head, 5, 1);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Inserting at position 1 means the new node becomes the head.
✗ Incorrect
The new node with value 5 is inserted at the head, so it appears first.
🔧 Debug
advancedIdentify the error in insertion at a specific position in singly linked list
What error will occur when running this code to insert at position 5 in a list of 3 nodes?
DSA C++
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
void insertAtPosition(Node*& head, int val, int pos) {
Node* newNode = new Node(val);
if (pos == 1) {
newNode->next = head;
head = newNode;
return;
}
Node* temp = head;
for (int i = 1; i < pos - 1; i++) {
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
}
int main() {
Node* head = new Node(1);
head->next = new Node(2);
head->next->next = new Node(3);
insertAtPosition(head, 99, 5);
return 0;
}Attempts:
2 left
💡 Hint
Check what happens when temp becomes nullptr in the loop.
✗ Incorrect
The loop does not check if temp is nullptr before accessing temp->next, causing a crash.
❓ Predict Output
advancedOutput after inserting at position beyond list length
What is the printed state of the linked list after attempting to insert 50 at position 10 in a list of 4 nodes?
DSA C++
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
void insertAtPosition(Node*& head, int val, int pos) {
Node* newNode = new Node(val);
if (pos == 1) {
newNode->next = head;
head = newNode;
return;
}
Node* temp = head;
for (int i = 1; i < pos - 1 && temp != nullptr; i++) {
temp = temp->next;
}
if (temp == nullptr) return; // position out of bounds
newNode->next = temp->next;
temp->next = newNode;
}
void printList(Node* head) {
while (head) {
std::cout << head->data << " -> ";
head = head->next;
}
std::cout << "null" << std::endl;
}
int main() {
Node* head = new Node(1);
head->next = new Node(2);
head->next->next = new Node(3);
head->next->next->next = new Node(4);
insertAtPosition(head, 50, 10);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Insertion should not happen if position is beyond list length.
✗ Incorrect
The function returns early if position is invalid, so list remains unchanged.
🧠 Conceptual
expertTime complexity of inserting at a specific position in singly linked list
What is the time complexity of inserting a node at position k in a singly linked list of length n?
Attempts:
2 left
💡 Hint
Think about how many nodes you must visit to reach position k.
✗ Incorrect
To insert at position k, you must traverse k-1 nodes, so time is proportional to k.
