Challenge - 5 Problems
Master of Insert at Middle Specific Position
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after inserting at middle position in linked list
What is the printed state of the linked list after inserting value 99 at position 3?
DSA Typescript
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } class LinkedList { head: Node | null = null; insertAtPosition(value: number, position: number): void { const newNode = new Node(value); if (position === 1) { newNode.next = this.head; this.head = newNode; return; } let current = this.head; let prev: Node | null = null; let count = 1; while (current !== null && count < position) { prev = current; current = current.next; count++; } if (prev !== null) { prev.next = newNode; newNode.next = current; } } printList(): string { let result = ''; let current = this.head; while (current !== null) { result += current.value + ' -> '; current = current.next; } return result + 'null'; } } const list = new LinkedList(); list.insertAtPosition(10, 1); list.insertAtPosition(20, 2); list.insertAtPosition(30, 3); list.insertAtPosition(99, 3); console.log(list.printList());
Attempts:
2 left
💡 Hint
Remember that position 3 means the new node should be placed after the second node.
✗ Incorrect
The insertAtPosition method inserts the new node at the given position. Position 3 means after the second node (value 20). So the new node with value 99 is placed between 20 and 30.
❓ Predict Output
intermediateResult of inserting at position beyond list length
What is the printed state of the linked list after inserting value 50 at position 10 in the list 5 -> 15 -> 25 -> null?
DSA Typescript
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } class LinkedList { head: Node | null = null; insertAtPosition(value: number, position: number): void { const newNode = new Node(value); if (position === 1) { newNode.next = this.head; this.head = newNode; return; } let current = this.head; let prev: Node | null = null; let count = 1; while (current !== null && count < position) { prev = current; current = current.next; count++; } if (prev !== null) { prev.next = newNode; newNode.next = current; } } printList(): string { let result = ''; let current = this.head; while (current !== null) { result += current.value + ' -> '; current = current.next; } return result + 'null'; } } const list = new LinkedList(); list.insertAtPosition(5, 1); list.insertAtPosition(15, 2); list.insertAtPosition(25, 3); list.insertAtPosition(50, 10); console.log(list.printList());
Attempts:
2 left
💡 Hint
If the position is beyond the list length, the new node is appended at the end.
✗ Incorrect
The insertAtPosition method inserts the new node at the given position or at the end if position is beyond list length. Here, 50 is appended after 25.
🔧 Debug
advancedIdentify the error in insertAtPosition method
What error will this code produce when inserting at position 1 in an empty linked list?
DSA Typescript
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } class LinkedList { head: Node | null = null; insertAtPosition(value: number, position: number): void { const newNode = new Node(value); if (position === 1) { newNode.next = this.head; this.head = newNode; return; } let current = this.head; let prev: Node | null = null; let count = 1; while (current !== null && count < position) { prev = current; current = current.next; count++; } prev.next = newNode; newNode.next = current; } } const list = new LinkedList(); list.insertAtPosition(100, 1); console.log(list.head?.value);
Attempts:
2 left
💡 Hint
Check the condition for position 1 and how head is updated.
✗ Incorrect
When position is 1, the method sets newNode.next to head (which is null) and updates head to newNode. No error occurs and head value is 100.
❓ Predict Output
advancedOutput after multiple insertions at various positions
What is the printed linked list after these insertions?
Insert 1 at position 1
Insert 3 at position 2
Insert 2 at position 2
Insert 4 at position 5
DSA Typescript
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } class LinkedList { head: Node | null = null; insertAtPosition(value: number, position: number): void { const newNode = new Node(value); if (position === 1) { newNode.next = this.head; this.head = newNode; return; } let current = this.head; let prev: Node | null = null; let count = 1; while (current !== null && count < position) { prev = current; current = current.next; count++; } if (prev !== null) { prev.next = newNode; newNode.next = current; } } printList(): string { let result = ''; let current = this.head; while (current !== null) { result += current.value + ' -> '; current = current.next; } return result + 'null'; } } const list = new LinkedList(); list.insertAtPosition(1, 1); list.insertAtPosition(3, 2); list.insertAtPosition(2, 2); list.insertAtPosition(4, 5); console.log(list.printList());
Attempts:
2 left
💡 Hint
Insertions at position 2 shift existing nodes to the right. Position 5 appends at the end.
✗ Incorrect
After inserting 1 at position 1: 1 -> null
Insert 3 at position 2: 1 -> 3 -> null
Insert 2 at position 2: 1 -> 2 -> 3 -> null
Insert 4 at position 5 (beyond length): appended at end: 1 -> 2 -> 3 -> 4 -> null
🧠 Conceptual
expertTime complexity of inserting at middle position in singly linked list
What is the time complexity of inserting a node at a specific middle position in a singly linked list of length n?
Attempts:
2 left
💡 Hint
Think about how you find the position before inserting in a singly linked list.
✗ Incorrect
In a singly linked list, to insert at a specific position, you must traverse nodes from the head to that position, which takes O(n) time. The insertion itself is O(1).
