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 value 3?
Given the linked list and search code below, what will be printed when searching for value 3?
DSA C#
public class Node { public int data; public Node next; public Node(int d) { data = d; next = null; } } public class LinkedList { public Node head; public bool Search(int key) { Node current = head; while (current != null) { if (current.data == key) return true; current = current.next; } return false; } } 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); bool found = list.Search(3); System.Console.WriteLine(found ? "Found" : "Not Found");
Attempts:
2 left
💡 Hint
Think about how the search method checks each node's data.
✗ Incorrect
The search method traverses the linked list and returns true when it finds the node with data 3. Since 3 is in the list, it prints "Found".
❓ Predict Output
intermediateWhat is the output after searching for value 5?
Given the linked list and search code below, what will be printed when searching for value 5?
DSA C#
public class Node { public int data; public Node next; public Node(int d) { data = d; next = null; } } public class LinkedList { public Node head; public bool Search(int key) { Node current = head; while (current != null) { if (current.data == key) return true; current = current.next; } return false; } } 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); bool found = list.Search(5); System.Console.WriteLine(found ? "Found" : "Not Found");
Attempts:
2 left
💡 Hint
Check if the value 5 exists in the linked list nodes.
✗ Incorrect
The search method returns false because the value 5 is not present in the linked list. So it prints "Not Found".
🔧 Debug
advancedWhat error does this search code produce?
Consider this search method in a linked list. What error will it cause when called?
DSA C#
public bool Search(int key) { Node current = head; while (current.next != null) { if (current.data == key) return true; current = current.next; } return false; }
Attempts:
2 left
💡 Hint
Think about what happens when current is the last node.
✗ Incorrect
The loop condition uses current.next != null, so when current is the last node, current.next is null and the loop stops before checking the last node's data. If the key is in the last node, it will not be found. Also, if the list is empty (head is null), current.next causes NullReferenceException.
🧠 Conceptual
advancedHow many nodes are checked in the search method?
In the search method below, how many nodes are checked when searching for a value in a linked list of 5 nodes?
DSA C#
public bool Search(int key) { Node current = head; while (current != null) { if (current.data == key) return true; current = current.next; } return false; }
Attempts:
2 left
💡 Hint
The loop stops when the value is found or all nodes are checked.
✗ Incorrect
The search checks nodes one by one until it finds the value or reaches the end. So it can check from 1 up to all 5 nodes depending on where the value is.
🚀 Application
expertWhat is the output after searching for value 7 in this linked list?
Given the linked list with nodes 10 -> 20 -> 30 -> 40 -> 50, and the search method below, what will be printed when searching for value 7?
DSA C#
public class Node { public int data; public Node next; public Node(int d) { data = d; next = null; } } public class LinkedList { public Node head; public bool Search(int key) { Node current = head; while (current != null) { if (current.data == key) return true; current = current.next; } return false; } } 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); bool found = list.Search(7); System.Console.WriteLine(found ? "Found" : "Not Found");
Attempts:
2 left
💡 Hint
Check if 7 is in the list nodes.
✗ Incorrect
The value 7 is not in the linked list nodes, so the search returns false and prints "Not Found".
