Challenge - 5 Problems
Insert at Middle Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after inserting at position 3?
Given the linked list 10 -> 20 -> 30 -> 40 -> null, what is the list after inserting 25 at position 3?
DSA C#
class Node { public int data; public Node next; public Node(int d) { data = d; next = null; } } class LinkedList { public Node head; public void InsertAtPosition(int data, int position) { Node newNode = new Node(data); if (position == 1) { newNode.next = head; head = newNode; return; } Node current = head; for (int i = 1; i < position - 1 && current != null; i++) { current = current.next; } if (current == null) return; newNode.next = current.next; current.next = newNode; } public override string ToString() { Node temp = head; string result = ""; while (temp != null) { result += temp.data + " -> "; temp = temp.next; } return result + "null"; } } LinkedList list = new LinkedList(); list.head = new Node(10); list.head.next = new Node(20); list.head.next.next = new Node(30); list.head.next.next.next = new Node(40); list.InsertAtPosition(25, 3); System.Console.WriteLine(list.ToString());
Attempts:
2 left
💡 Hint
Remember that position 3 means after the second node.
✗ Incorrect
The insertion at position 3 places the new node after the second node (20), so the new node with data 25 comes before the original third node (30).
❓ Predict Output
intermediateWhat happens if you insert at position 1?
Given the linked list 5 -> 15 -> 25 -> null, what is the list after inserting 1 at position 1?
DSA C#
class Node { public int data; public Node next; public Node(int d) { data = d; next = null; } } class LinkedList { public Node head; public void InsertAtPosition(int data, int position) { Node newNode = new Node(data); if (position == 1) { newNode.next = head; head = newNode; return; } Node current = head; for (int i = 1; i < position - 1 && current != null; i++) { current = current.next; } if (current == null) return; newNode.next = current.next; current.next = newNode; } public override string ToString() { Node temp = head; string result = ""; while (temp != null) { result += temp.data + " -> "; temp = temp.next; } return result + "null"; } } LinkedList list = new LinkedList(); list.head = new Node(5); list.head.next = new Node(15); list.head.next.next = new Node(25); list.InsertAtPosition(1, 1); System.Console.WriteLine(list.ToString());
Attempts:
2 left
💡 Hint
Inserting at position 1 means adding at the start.
✗ Incorrect
Inserting at position 1 places the new node at the head, pushing the existing nodes forward.
🔧 Debug
advancedWhy does this insertion code fail to insert at position 4?
Consider this code snippet to insert at position 4 in a linked list. Why does it fail to insert the new node?
DSA C#
public void InsertAtPosition(int data, int position) { Node newNode = new Node(data); Node current = head; for (int i = 1; i < position - 1; i++) { current = current.next; } newNode.next = current.next; current.next = newNode; }
Attempts:
2 left
💡 Hint
Think about what happens if position is beyond the list length.
✗ Incorrect
If position is beyond the list length, current becomes null and accessing current.next causes a NullReferenceException.
🧠 Conceptual
advancedWhat is the time complexity of inserting at a specific middle position in a singly linked list?
If you insert a node at position k in a singly linked list of length n, what is the time complexity?
Attempts:
2 left
💡 Hint
Think about how many nodes you must visit before inserting.
✗ Incorrect
You must reach the node before the insertion point, which takes O(k) time in a singly linked list.
❓ Predict Output
expertWhat is the final list after multiple insertions at various positions?
Starting with an empty list, perform these insertions in order:
1) Insert 100 at position 1
2) Insert 200 at position 2
3) Insert 150 at position 2
4) Insert 50 at position 1
What is the final linked list?
DSA C#
class Node { public int data; public Node next; public Node(int d) { data = d; next = null; } } class LinkedList { public Node head; public void InsertAtPosition(int data, int position) { Node newNode = new Node(data); if (position == 1) { newNode.next = head; head = newNode; return; } Node current = head; for (int i = 1; i < position - 1 && current != null; i++) { current = current.next; } if (current == null) return; newNode.next = current.next; current.next = newNode; } public override string ToString() { Node temp = head; string result = ""; while (temp != null) { result += temp.data + " -> "; temp = temp.next; } return result + "null"; } } LinkedList list = new LinkedList(); list.InsertAtPosition(100, 1); list.InsertAtPosition(200, 2); list.InsertAtPosition(150, 2); list.InsertAtPosition(50, 1); System.Console.WriteLine(list.ToString());
Attempts:
2 left
💡 Hint
Track each insertion step carefully from the head.
✗ Incorrect
Insertions update the head or next pointers accordingly, resulting in the final list: 50 -> 100 -> 150 -> 200 -> null.
