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?
Consider the linked list created by the code below. What will be printed when searching for the value 3?
DSA Java
class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } } class LinkedList { Node head; void add(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; } else { Node current = head; while (current.next != null) { current = current.next; } current.next = newNode; } } boolean search(int value) { Node current = head; while (current != null) { if (current.data == value) { return true; } current = current.next; } return false; } } public class Main { public static void main(String[] args) { LinkedList list = new LinkedList(); list.add(1); list.add(2); list.add(3); list.add(4); System.out.println(list.search(3)); } }
Attempts:
2 left
💡 Hint
Think about whether the value 3 exists in the linked list nodes.
✗ Incorrect
The search method checks each node's data. Since 3 is added to the list, the method returns true and prints it.
❓ Predict Output
intermediateWhat is the output after searching for value 5?
Given the linked list below, what will be printed when searching for the value 5?
DSA Java
class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } } class LinkedList { Node head; void add(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; } else { Node current = head; while (current.next != null) { current = current.next; } current.next = newNode; } } boolean search(int value) { Node current = head; while (current != null) { if (current.data == value) { return true; } current = current.next; } return false; } } public class Main { public static void main(String[] args) { LinkedList list = new LinkedList(); list.add(1); list.add(2); list.add(3); list.add(4); System.out.println(list.search(5)); } }
Attempts:
2 left
💡 Hint
Check if the value 5 was added to the list.
✗ Incorrect
Since 5 was not added to the list, the search method returns false and prints it.
🧠 Conceptual
advancedWhat is the time complexity of searching a value in a singly linked list?
If a singly linked list has n nodes, what is the time complexity of searching for a value in the list?
Attempts:
2 left
💡 Hint
Think about how many nodes you might need to check in the worst case.
✗ Incorrect
In the worst case, you may need to check every node once, so the time complexity is linear O(n).
🔧 Debug
advancedWhat error occurs when searching in an empty linked list?
Consider the search method below. What happens if the linked list is empty (head is null) and we call search(1)?
DSA Java
boolean search(int value) { Node current = head; while (current != null) { if (current.data == value) { return true; } current = current.next; } return false; }
Attempts:
2 left
💡 Hint
Check what happens when current is null at the start.
✗ Incorrect
If head is null, current is null, so the while loop does not run and the method returns false safely.
🚀 Application
expertHow many nodes are visited when searching for value 4 in the list 1->2->3->4->5?
Given the linked list 1 -> 2 -> 3 -> 4 -> 5 -> null, how many nodes does the search method visit when searching for the value 4?
Attempts:
2 left
💡 Hint
Count nodes from head until the value 4 is found.
✗ Incorrect
The search visits nodes with values 1, 2, 3, and 4, so it visits 4 nodes before returning true.
