Challenge - 5 Problems
Linked List Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after searching for a value in the linked list?
Consider the following C++ code that creates a linked list and searches for a value. What will be printed?
DSA C++
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
bool search(Node* head, int key) {
Node* current = head;
while (current != nullptr) {
if (current->data == key) {
return true;
}
current = current->next;
}
return false;
}
int main() {
Node* head = new Node(10);
head->next = new Node(20);
head->next->next = new Node(30);
if (search(head, 20)) {
std::cout << "Found" << std::endl;
} else {
std::cout << "Not Found" << std::endl;
}
return 0;
}Attempts:
2 left
💡 Hint
Check if the value 20 exists in the linked list nodes.
✗ Incorrect
The linked list contains nodes with values 10, 20, and 30. The search function checks each node's data. Since 20 is present, it returns true and prints 'Found'.
❓ Predict Output
intermediateWhat is the output when searching for a missing value?
Given the linked list below, what will be the output when searching for the value 40?
DSA C++
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
bool search(Node* head, int key) {
Node* current = head;
while (current != nullptr) {
if (current->data == key) {
return true;
}
current = current->next;
}
return false;
}
int main() {
Node* head = new Node(5);
head->next = new Node(15);
head->next->next = new Node(25);
if (search(head, 40)) {
std::cout << "Found" << std::endl;
} else {
std::cout << "Not Found" << std::endl;
}
return 0;
}Attempts:
2 left
💡 Hint
Check if the value 40 is present in the linked list nodes.
✗ Incorrect
The linked list contains 5, 15, and 25. Since 40 is not in the list, the search returns false and prints 'Not Found'.
🔧 Debug
advancedWhat error does this code produce when searching an empty linked list?
Analyze the code below. What error will occur when searching for a value in an empty linked list?
DSA C++
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
bool search(Node* head, int key) {
Node* current = head;
while (current != nullptr) {
if (current->data == key) {
return true;
}
current = current->next;
}
return false;
}
int main() {
Node* head = nullptr;
if (search(head, 10)) {
std::cout << "Found" << std::endl;
} else {
std::cout << "Not Found" << std::endl;
}
return 0;
}Attempts:
2 left
💡 Hint
Consider what happens when head is nullptr and the while loop condition.
✗ Incorrect
The search function checks if current is nullptr before accessing data. Since head is nullptr, the loop does not run and returns false safely.
🧠 Conceptual
advancedHow many nodes are checked when searching for a value not present in the list?
If a linked list has 4 nodes and the searched value is not present, how many nodes does the search function check before returning false?
Attempts:
2 left
💡 Hint
The search checks each node until it reaches the end.
✗ Incorrect
The search function visits every node to check its data. For 4 nodes, it checks all 4 before concluding the value is not found.
🚀 Application
expertWhat is the output after searching for a value in a circular linked list?
Consider a circular linked list where the last node points back to the head. The search function below is used to find a value. What will happen when searching for 15?
DSA C++
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
bool search(Node* head, int key) {
Node* current = head;
if (head == nullptr) return false;
do {
if (current->data == key) {
return true;
}
current = current->next;
} while (current != head);
return false;
}
int main() {
Node* head = new Node(10);
Node* second = new Node(15);
Node* third = new Node(20);
head->next = second;
second->next = third;
third->next = head; // circular link
if (search(head, 15)) {
std::cout << "Found" << std::endl;
} else {
std::cout << "Not Found" << std::endl;
}
return 0;
}Attempts:
2 left
💡 Hint
The search uses a do-while loop to handle circular lists.
✗ Incorrect
The search function uses a do-while loop that stops when it returns to the head. It finds 15 in the second node and returns true, printing 'Found'.
