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 of searching for value 3 in this linked list?
Consider the linked list created by inserting values 1, 2, 3, 4 in order. What will be the output of searching for value 3?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } class LinkedList { constructor() { this.head = null; } insert(value) { const newNode = new Node(value); if (!this.head) { this.head = newNode; return; } let current = this.head; while (current.next) { current = current.next; } current.next = newNode; } search(value) { let current = this.head; while (current) { if (current.value === value) { return true; } current = current.next; } return false; } } const list = new LinkedList(); list.insert(1); list.insert(2); list.insert(3); list.insert(4); console.log(list.search(3));
Attempts:
2 left
💡 Hint
Think about how the search method checks each node's value.
✗ Incorrect
The search method traverses the linked list and returns true when it finds the value 3.
❓ Predict Output
intermediateWhat is the output when searching for a value not in the list?
Given the linked list with values 5, 10, 15, what is the output of searching for value 7?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } class LinkedList { constructor() { this.head = null; } insert(value) { const newNode = new Node(value); if (!this.head) { this.head = newNode; return; } let current = this.head; while (current.next) { current = current.next; } current.next = newNode; } search(value) { let current = this.head; while (current) { if (current.value === value) { return true; } current = current.next; } return false; } } const list = new LinkedList(); list.insert(5); list.insert(10); list.insert(15); console.log(list.search(7));
Attempts:
2 left
💡 Hint
If the value is not found, what does the search method return?
✗ Incorrect
The search method returns false because 7 is not in the list.
🔧 Debug
advancedWhy does this search method cause an infinite loop?
Identify the bug causing an infinite loop in the search method below.
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } class LinkedList { constructor() { this.head = null; } insert(value) { const newNode = new Node(value); if (!this.head) { this.head = newNode; return; } let current = this.head; while (current.next) { current = current.next; } current.next = newNode; } search(value) { let current = this.head; while (current && current.value !== value) { current = current.next; } return current !== null; } }
Attempts:
2 left
💡 Hint
Consider what happens when the value is not found in the list.
✗ Incorrect
The search method tries to access current.value without checking if current is null, causing an infinite loop or error.
🚀 Application
advancedHow many nodes are traversed when searching for value 4 in this list?
Given a linked list with nodes containing values 1 -> 2 -> 3 -> 4 -> 5, how many nodes does the search method check when searching for value 4?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } class LinkedList { constructor() { this.head = null; } insert(value) { const newNode = new Node(value); if (!this.head) { this.head = newNode; return; } let current = this.head; while (current.next) { current = current.next; } current.next = newNode; } search(value) { let current = this.head; let count = 0; while (current) { count++; if (current.value === value) { break; } current = current.next; } return count; } } const list = new LinkedList(); list.insert(1); list.insert(2); list.insert(3); list.insert(4); list.insert(5); console.log(list.search(4));
Attempts:
2 left
💡 Hint
Count nodes checked until the value is found.
✗ Incorrect
The search checks nodes with values 1, 2, 3, and 4, so 4 nodes are traversed.
🧠 Conceptual
expertWhat is the time complexity of searching for a value in a singly linked list?
Consider a singly linked list with n nodes. What is the worst-case time complexity of searching for a value in this 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, so the time complexity is O(n).
