Challenge - 5 Problems
Middle Element Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateFind the middle element output
What will be the output of the following code that finds the middle element of a linked list?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } class LinkedList { constructor() { this.head = null; } append(value) { const newNode = new Node(value); if (!this.head) { this.head = newNode; return; } let current = this.head; while (current.next) { current = current.next; } current.next = newNode; } findMiddle() { let slow = this.head; let fast = this.head; while (fast && fast.next) { slow = slow.next; fast = fast.next.next; } return slow ? slow.value : null; } } const list = new LinkedList(); [10, 20, 30, 40, 50].forEach(num => list.append(num)); console.log(list.findMiddle());
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
intermediateMiddle element with even number of nodes
What will be the output of the code when the linked list has an even number of nodes?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } class LinkedList { constructor() { this.head = null; } append(value) { const newNode = new Node(value); if (!this.head) { this.head = newNode; return; } let current = this.head; while (current.next) { current = current.next; } current.next = newNode; } findMiddle() { let slow = this.head; let fast = this.head; while (fast && fast.next) { slow = slow.next; fast = fast.next.next; } return slow ? slow.value : null; } } const list = new LinkedList(); [5, 10, 15, 20, 25, 30].forEach(num => list.append(num)); console.log(list.findMiddle());
Attempts:
2 left
💡 Hint
For even length, the middle is the second of the two middle nodes.
✗ Incorrect
When the list has even nodes, the slow pointer ends at the second middle node.
🔧 Debug
advancedIdentify the error in middle element code
What error will this code produce when trying to find the middle element of a linked list?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } class LinkedList { constructor() { this.head = null; } append(value) { const newNode = new Node(value); if (!this.head) { this.head = newNode; return; } let current = this.head; while (current.next) { current = current.next; } current.next = newNode; } findMiddle() { let slow = this.head; let fast = this.head; while (fast && fast.next && fast.next.next) { slow = slow.next; fast = fast.next.next; } return slow.value; } } const list = new LinkedList(); [1].forEach(num => list.append(num)); console.log(list.findMiddle());
Attempts:
2 left
💡 Hint
Check the while loop condition when list has only one node.
✗ Incorrect
The condition fast.next.next fails when fast.next is null, causing a TypeError.
🧠 Conceptual
advancedWhy use two pointers to find middle?
Why does using two pointers (slow and fast) help find the middle element of a linked list efficiently?
Attempts:
2 left
💡 Hint
Think about relative speeds of pointers.
✗ Incorrect
The fast pointer moves two steps at a time, so when it reaches the end, the slow pointer is at the middle.
❓ Predict Output
expertOutput after multiple middle element calls
What will be the output after these operations on the linked list?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } class LinkedList { constructor() { this.head = null; } append(value) { const newNode = new Node(value); if (!this.head) { this.head = newNode; return; } let current = this.head; while (current.next) { current = current.next; } current.next = newNode; } findMiddle() { let slow = this.head; let fast = this.head; while (fast && fast.next) { slow = slow.next; fast = fast.next.next; } return slow ? slow.value : null; } } const list = new LinkedList(); [1, 2, 3, 4, 5].forEach(num => list.append(num)); const firstMiddle = list.findMiddle(); list.append(6); const secondMiddle = list.findMiddle(); console.log([firstMiddle, secondMiddle]);
Attempts:
2 left
💡 Hint
Check middle for odd and then even length list.
✗ Incorrect
Initially, list has 5 elements, middle is 3. After adding 6th element, middle is 4.
