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 is the output of the following Java code that finds the middle element of a linked list?
DSA Java
class Node { int data; Node next; Node(int d) { data = d; next = null; } } public class LinkedList { Node head; int findMiddle() { Node slow = head; Node fast = head; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } return slow.data; } 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.head.next.next.next.next = new Node(50); System.out.println(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. For 5 nodes, middle is the 3rd node with value 30.
❓ 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 Java
class Node { int data; Node next; Node(int d) { data = d; next = null; } } public class LinkedList { Node head; int findMiddle() { Node slow = head; Node fast = head; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } return slow.data; } 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); System.out.println(list.findMiddle()); } }
Attempts:
2 left
💡 Hint
For even nodes, the slow pointer ends at the second middle node.
✗ Incorrect
With 4 nodes, the slow pointer moves to the 2nd node (20), then 3rd node (30) when fast reaches the end. So the middle is 30.
🔧 Debug
advancedIdentify the error in middle element code
What error does the following code produce when trying to find the middle element of a linked list?
DSA Java
class Node { int data; Node next; Node(int d) { data = d; next = null; } } public class LinkedList { Node head; int findMiddle() { Node slow = head; Node fast = head.next; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } return slow.data; } 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); System.out.println(list.findMiddle()); } }
Attempts:
2 left
💡 Hint
Check how fast pointer is initialized and moved.
✗ Incorrect
Fast pointer starts at head.next. For 3 nodes, fast moves two steps and becomes null, but next iteration tries to access fast.next causing NullPointerException.
🧠 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
Fast pointer moves two nodes per step, slow moves one. When fast reaches end, slow is at middle. This avoids counting nodes or extra passes.
❓ Predict Output
expertOutput after multiple middle element calls
What is the output of the following code that calls findMiddle() after removing nodes?
DSA Java
class Node { int data; Node next; Node(int d) { data = d; next = null; } } public class LinkedList { Node head; int findMiddle() { Node slow = head; Node fast = head; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } return slow.data; } void removeFirst() { if (head != null) head = head.next; } public static void main(String[] args) { LinkedList list = new LinkedList(); list.head = new Node(1); list.head.next = new Node(2); list.head.next.next = new Node(3); list.head.next.next.next = new Node(4); list.head.next.next.next.next = new Node(5); System.out.println(list.findMiddle()); list.removeFirst(); System.out.println(list.findMiddle()); list.removeFirst(); System.out.println(list.findMiddle()); } }
Attempts:
2 left
💡 Hint
Trace the list after each removal and find the middle.
✗ Incorrect
Initially list is 1->2->3->4->5, middle is 3.
After removing 1, list is 2->3->4->5, middle is 3.
After removing 2, list is 3->4->5, middle is 4.
