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 linked list
What is the printed linked list after inserting value 99 at position 3?
DSA Java
class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } } class LinkedList { Node head; 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; } void printList() { Node temp = head; while (temp != null) { System.out.print(temp.data + " -> "); temp = temp.next; } System.out.print("null\n"); } } public class Main { public static void main(String[] args) { 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(99, 3); list.printList(); } }
Attempts:
2 left
💡 Hint
Remember, position 3 means the new node should be the third node in the list.
✗ Incorrect
The insertAtPosition method inserts the new node at the given position by adjusting the next pointers. Position 3 means after the second node, so 99 comes after 20 and before 30.
❓ Predict Output
intermediateResult after inserting at position 1 (head) in a linked list
What is the printed linked list after inserting value 5 at position 1?
DSA Java
class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } } class LinkedList { Node head; 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; } void printList() { Node temp = head; while (temp != null) { System.out.print(temp.data + " -> "); temp = temp.next; } System.out.print("null\n"); } } public class Main { public static void main(String[] args) { LinkedList list = new LinkedList(); list.head = new Node(10); list.head.next = new Node(20); list.head.next.next = new Node(30); list.insertAtPosition(5, 1); list.printList(); } }
Attempts:
2 left
💡 Hint
Inserting at position 1 means the new node becomes the head.
✗ Incorrect
When inserting at position 1, the new node points to the current head, and then becomes the new head.
🔧 Debug
advancedIdentify the error in insertAtPosition method
What error will occur when running this code if position is greater than the list length?
DSA Java
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; i++) { current = current.next; } newNode.next = current.next; current.next = newNode; }
Attempts:
2 left
💡 Hint
Consider what happens if current becomes null before loop ends.
✗ Incorrect
If position is larger than list length + 1, current becomes null and accessing current.next causes NullPointerException.
🧠 Conceptual
advancedTime 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 from head to (k-1)th node, which takes O(k) time.
🚀 Application
expertResult after multiple insertions at various positions
Given an initially empty linked list, what is the final printed list after these insertions?
1) insert 10 at position 1
2) insert 20 at position 2
3) insert 15 at position 2
4) insert 5 at position 1
5) insert 25 at position 5
DSA Java
class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } } class LinkedList { Node head; 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; } void printList() { Node temp = head; while (temp != null) { System.out.print(temp.data + " -> "); temp = temp.next; } System.out.print("null\n"); } } public class Main { public static void main(String[] args) { LinkedList list = new LinkedList(); list.insertAtPosition(10, 1); list.insertAtPosition(20, 2); list.insertAtPosition(15, 2); list.insertAtPosition(5, 1); list.insertAtPosition(25, 5); list.printList(); } }
Attempts:
2 left
💡 Hint
Insertions shift nodes to the right; track positions carefully.
✗ Incorrect
Step-by-step:
1) 10 at pos 1: 10
2) 20 at pos 2: 10 -> 20
3) 15 at pos 2: 10 -> 15 -> 20
4) 5 at pos 1: 5 -> 10 -> 15 -> 20
5) 25 at pos 5: 5 -> 10 -> 15 -> 20 -> 25
