Challenge - 5 Problems
Linked List Middle Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateFind the middle element of a linked list
What is the output of the following TypeScript code that finds the middle element of a linked list?
DSA Typescript
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } function findMiddle(head: Node | null): number | null { let slow = head; let fast = head; while (fast !== null && fast.next !== null) { slow = slow!.next; fast = fast.next.next; } return slow ? slow.value : null; } // Create linked list: 10 -> 20 -> 30 -> 40 -> 50 -> null const head = new Node(10); head.next = new Node(20); head.next.next = new Node(30); head.next.next.next = new Node(40); head.next.next.next.next = new Node(50); console.log(findMiddle(head));
Attempts:
2 left
💡 Hint
Use two pointers: one moves twice as fast as the other.
✗ Incorrect
The slow pointer moves one step at a time, the fast pointer moves two steps. When fast reaches the end, slow is at the middle.
❓ Predict Output
intermediateFind middle element in even-length linked list
What is the output of this code that finds the middle element in an even-length linked list?
DSA Typescript
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } function findMiddle(head: Node | null): number | null { let slow = head; let fast = head; while (fast !== null && fast.next !== null) { slow = slow!.next; fast = fast.next.next; } return slow ? slow.value : null; } // Create linked list: 1 -> 2 -> 3 -> 4 -> null const head = new Node(1); head.next = new Node(2); head.next.next = new Node(3); head.next.next.next = new Node(4); console.log(findMiddle(head));
Attempts:
2 left
💡 Hint
For even length, the middle is the second of the two middle nodes.
✗ Incorrect
The slow pointer ends at the third node (value 3) when fast pointer reaches the end.
🔧 Debug
advancedIdentify the error in finding middle element
What error does this TypeScript code produce when trying to find the middle element of a linked list?
DSA Typescript
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } function findMiddle(head: Node | null): number | null { let slow = head; let fast = head; while (fast !== null && fast.next !== null) { slow = slow.next; fast = fast.next.next; } return slow.value; } const head = null; console.log(findMiddle(head));
Attempts:
2 left
💡 Hint
Check if slow can be null before accessing value.
✗ Incorrect
If the list is empty or slow becomes null, accessing slow.value causes a TypeError.
🧠 Conceptual
advancedWhy use two pointers to find middle element?
Why is the two-pointer technique (slow and fast pointers) efficient for finding the middle element of a linked list?
Attempts:
2 left
💡 Hint
Think about how many times the list is traversed and memory used.
✗ Incorrect
The two-pointer method moves through the list once, with one pointer moving twice as fast, so it finds the middle in one pass using constant extra space.
🚀 Application
expertFind middle element after modifying linked list
Given this linked list and operations, what is the middle element after all operations?
DSA Typescript
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } function findMiddle(head: Node | null): number | null { let slow = head; let fast = head; while (fast !== null && fast.next !== null) { slow = slow!.next; fast = fast.next.next; } return slow ? slow.value : null; } // Create linked list: 1 -> 3 -> 5 -> 7 -> 9 -> null const head = new Node(1); head.next = new Node(3); head.next.next = new Node(5); head.next.next.next = new Node(7); head.next.next.next.next = new Node(9); // Insert 6 after 5 let current = head; while (current !== null && current.value !== 5) { current = current.next; } if (current !== null) { const newNode = new Node(6); newNode.next = current.next; current.next = newNode; } // Remove node with value 3 let prev = null; current = head; while (current !== null && current.value !== 3) { prev = current; current = current.next; } if (prev !== null && current !== null) { prev.next = current.next; } console.log(findMiddle(head));
Attempts:
2 left
💡 Hint
Update the list carefully after insert and delete, then find the middle.
✗ Incorrect
After inserting 6 after 5 and removing 3, the list is 1 -> 5 -> 6 -> 7 -> 9. The middle is the third element, 6.
