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 for odd length list
What is the output of the following C code that finds the middle element of a linked list with 5 nodes?
DSA C
#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; void push(struct Node** head_ref, int new_data) { struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); new_node->data = new_data; new_node->next = (*head_ref); (*head_ref) = new_node; } int printMiddle(struct Node* head) { struct Node *slow_ptr = head; struct Node *fast_ptr = head; if (head != NULL) { while (fast_ptr != NULL && fast_ptr->next != NULL) { fast_ptr = fast_ptr->next->next; slow_ptr = slow_ptr->next; } return slow_ptr->data; } return -1; } int main() { struct Node* head = NULL; push(&head, 5); push(&head, 4); push(&head, 3); push(&head, 2); push(&head, 1); int middle = printMiddle(head); printf("%d\n", middle); return 0; }
Attempts:
2 left
💡 Hint
Remember the slow pointer moves one step while the fast pointer moves two steps.
✗ Incorrect
The slow pointer reaches the middle node when the fast pointer reaches the end. For 5 nodes, the middle is the 3rd node with value 3.
❓ Predict Output
intermediate2:00remaining
Find the middle element output for even length list
What is the output of the following C code that finds the middle element of a linked list with 6 nodes?
DSA C
#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; void push(struct Node** head_ref, int new_data) { struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); new_node->data = new_data; new_node->next = (*head_ref); (*head_ref) = new_node; } int printMiddle(struct Node* head) { struct Node *slow_ptr = head; struct Node *fast_ptr = head; if (head != NULL) { while (fast_ptr != NULL && fast_ptr->next != NULL) { fast_ptr = fast_ptr->next->next; slow_ptr = slow_ptr->next; } return slow_ptr->data; } return -1; } int main() { struct Node* head = NULL; push(&head, 6); push(&head, 5); push(&head, 4); push(&head, 3); push(&head, 2); push(&head, 1); int middle = printMiddle(head); printf("%d\n", middle); return 0; }
Attempts:
2 left
💡 Hint
For even length lists, the slow pointer stops at the second middle node.
✗ Incorrect
The list is 1->2->3->4->5->6. The middle is the 4th node with value 4 because the slow pointer moves one step while fast moves two steps.
🔧 Debug
advanced2:00remaining
Identify the error in middle element function
What error will the following C code produce when trying to find the middle element of a linked list?
DSA C
#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; int printMiddle(struct Node* head) { struct Node *slow_ptr = head; struct Node *fast_ptr = head; while (fast_ptr->next != NULL && fast_ptr->next->next != NULL) { slow_ptr = slow_ptr->next; fast_ptr = fast_ptr->next->next; } return slow_ptr->data; } int main() { struct Node* head = NULL; // Empty list int middle = printMiddle(head); printf("%d\n", middle); return 0; }
Attempts:
2 left
💡 Hint
Check what happens when head is NULL and you access fast_ptr->next.
✗ Incorrect
The code does not check if head is NULL before accessing fast_ptr->next, causing a segmentation fault when the list is empty.
🧠 Conceptual
advanced1:30remaining
Why use two pointers to find middle element?
Why does the two-pointer technique (slow and fast pointers) efficiently find the middle element of a linked list?
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 has moved half the distance, landing on the middle node.
❓ Predict Output
expert2:30remaining
Output of middle element after list modification
What is the output of the following C code after modifying the linked list and finding the middle element?
DSA C
#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; void push(struct Node** head_ref, int new_data) { struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); new_node->data = new_data; new_node->next = (*head_ref); (*head_ref) = new_node; } int printMiddle(struct Node* head) { struct Node *slow_ptr = head; struct Node *fast_ptr = head; if (head != NULL) { while (fast_ptr != NULL && fast_ptr->next != NULL) { fast_ptr = fast_ptr->next->next; slow_ptr = slow_ptr->next; } return slow_ptr->data; } return -1; } int main() { struct Node* head = NULL; push(&head, 10); push(&head, 20); push(&head, 30); push(&head, 40); push(&head, 50); // List is 50->40->30->20->10 // Remove head node struct Node* temp = head; head = head->next; free(temp); // Now list is 40->30->20->10 int middle = printMiddle(head); printf("%d\n", middle); return 0; }
Attempts:
2 left
💡 Hint
After removing the first node, find the middle of the new list with 4 nodes.
✗ Incorrect
The new list is 40->30->20->10. The middle for even length is the second middle node, which is 20.
