Challenge - 5 Problems
Middle Element Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Find the middle element output
What is the output of the following code that finds the middle element of a linked list?
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None def find_middle(head): slow = head fast = head while fast and fast.next: slow = slow.next fast = fast.next.next return slow.data # Linked list: 10 -> 20 -> 30 -> 40 -> 50 -> None head = Node(10) head.next = Node(20) head.next.next = Node(30) head.next.next.next = Node(40) head.next.next.next.next = Node(50) print(find_middle(head))
Attempts:
2 left
💡 Hint
Use two pointers: one moves one step, the other moves two steps.
✗ 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 the list 10 -> 20 -> 30 -> 40 -> 50, the middle is 30.
❓ Predict Output
intermediate2:00remaining
Middle element in even length list
What is the output of the code when the linked list has an even number of elements?
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None def find_middle(head): slow = head fast = head while fast and fast.next: slow = slow.next fast = fast.next.next return slow.data # Linked list: 1 -> 2 -> 3 -> 4 -> None head = Node(1) head.next = Node(2) head.next.next = Node(3) head.next.next.next = Node(4) print(find_middle(head))
Attempts:
2 left
💡 Hint
When even number of nodes, the function returns the second middle node.
✗ Incorrect
For even length lists, the slow pointer ends at the second middle node.
Here, the middle nodes are 2 and 3, so output is 3.
🔧 Debug
advanced2:00remaining
Identify 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 Python
class Node: def __init__(self, data): self.data = data self.next = None def find_middle(head): slow = head fast = head while fast.next and fast.next.next: slow = slow.next fast = fast.next.next return slow.data head = None print(find_middle(head))
Attempts:
2 left
💡 Hint
Check the while loop condition for short lists.
✗ Incorrect
The condition 'while fast.next and fast.next.next' fails when fast is None, causing AttributeError when accessing fast.next.
🧠 Conceptual
advanced1:30remaining
Why use two pointers to find middle?
Why is the two-pointer technique efficient for finding the middle element of a linked list?
Attempts:
2 left
💡 Hint
Think about how many times the list is traversed.
✗ Incorrect
The two-pointer method finds the middle in one pass by moving one pointer twice as fast as the other.
This avoids counting or extra memory.
❓ Predict Output
expert2:30remaining
Output of middle element after list modification
What is the output after modifying the linked list and then finding the middle element?
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None def find_middle(head): slow = head fast = head while fast and fast.next: slow = slow.next fast = fast.next.next return slow.data head = Node(1) head.next = Node(2) head.next.next = Node(3) head.next.next.next = Node(4) head.next.next.next.next = Node(5) # Remove the third node (data=3) head.next.next = head.next.next.next print(find_middle(head))
Attempts:
2 left
💡 Hint
After removal, the list is 1 -> 2 -> 4 -> 5.
✗ Incorrect
After removing node with data 3, the list is 1 -> 2 -> 4 -> 5.
The middle nodes are 2 and 4, so the function returns the second middle, 4.