Challenge - 5 Problems
Linked List Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this linked list search code?
Given the linked list and search function below, what will be printed when searching for value 3?
DSA C
typedef struct Node {
int data;
struct Node* next;
} Node;
#include <stdio.h>
#include <stdlib.h>
int search(Node* head, int val) {
Node* current = head;
while (current != NULL) {
if (current->data == val) {
return 1;
}
current = current->next;
}
return 0;
}
int main() {
Node* head = malloc(sizeof(Node));
head->data = 1;
head->next = malloc(sizeof(Node));
head->next->data = 2;
head->next->next = malloc(sizeof(Node));
head->next->next->data = 3;
head->next->next->next = NULL;
if (search(head, 3)) {
printf("Found\n");
} else {
printf("Not Found\n");
}
return 0;
}Attempts:
2 left
💡 Hint
Trace the linked list nodes and check if the value 3 exists.
✗ Incorrect
The linked list contains nodes with values 1, 2, and 3. The search function returns 1 when it finds the value 3, so 'Found' is printed.
❓ Predict Output
intermediate2:00remaining
What is the output when searching for a value not in the list?
What will the program print when searching for value 5 in the linked list below?
DSA C
typedef struct Node {
int data;
struct Node* next;
} Node;
#include <stdio.h>
#include <stdlib.h>
int search(Node* head, int val) {
Node* current = head;
while (current != NULL) {
if (current->data == val) {
return 1;
}
current = current->next;
}
return 0;
}
int main() {
Node* head = malloc(sizeof(Node));
head->data = 10;
head->next = malloc(sizeof(Node));
head->next->data = 20;
head->next->next = malloc(sizeof(Node));
head->next->next->data = 30;
head->next->next->next = NULL;
if (search(head, 5)) {
printf("Found\n");
} else {
printf("Not Found\n");
}
return 0;
}Attempts:
2 left
💡 Hint
Check if 5 is present in the list nodes.
✗ Incorrect
The linked list contains 10, 20, and 30. Since 5 is not in the list, the search returns 0 and prints 'Not Found'.
🔧 Debug
advanced2:00remaining
What error does this linked list search code cause?
What error will occur when running this code?
DSA C
typedef struct Node {
int data;
struct Node* next;
} Node;
#include <stdio.h>
int search(Node* head, int val) {
Node* current = head;
while (current->next != NULL) {
if (current->data == val) {
return 1;
}
current = current->next;
}
return 0;
}
int main() {
Node node3 = {3, NULL};
Node node2 = {2, &node3};
Node node1 = {1, &node2};
if (search(&node1, 3)) {
printf("Found\n");
} else {
printf("Not Found\n");
}
return 0;
}Attempts:
2 left
💡 Hint
Look at the loop condition and what happens when current is the last node.
✗ Incorrect
The loop condition stops when current->next is NULL, so it never checks the last node's data. If the value is in the last node, it is missed. Also, if the value is not found, current becomes NULL and accessing current->next causes a segmentation fault.
🧠 Conceptual
advanced1:30remaining
Why is the loop condition 'while(current != NULL)' preferred in linked list search?
Choose the best reason why 'while(current != NULL)' is used instead of 'while(current->next != NULL)' in linked list search.
Attempts:
2 left
💡 Hint
Think about whether the last node's data is checked.
✗ Incorrect
Using 'while(current != NULL)' ensures every node, including the last, is checked. Using 'while(current->next != NULL)' skips the last node, possibly missing the value.
🚀 Application
expert2:30remaining
What is the output after searching for value 7 in this linked list?
Given the linked list created dynamically and the search function, what will be printed when searching for value 7?
DSA C
#include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* next; } Node; Node* createNode(int val) { Node* newNode = malloc(sizeof(Node)); newNode->data = val; newNode->next = NULL; return newNode; } int search(Node* head, int val) { Node* current = head; while (current != NULL) { if (current->data == val) { return 1; } current = current->next; } return 0; } int main() { Node* head = createNode(5); head->next = createNode(7); head->next->next = createNode(9); if (search(head, 7)) { printf("Found\n"); } else { printf("Not Found\n"); } return 0; }
Attempts:
2 left
💡 Hint
Check if the value 7 is in any node's data.
✗ Incorrect
The linked list nodes contain 5, 7, and 9. The search finds 7 in the second node and returns 1, printing 'Found'.
