Challenge - 5 Problems
Linked List Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Searching a Value in a Linked List
What is the output of the following Go code that searches for a value in a singly linked list?
DSA Go
package main import "fmt" type Node struct { value int next *Node } func search(head *Node, target int) bool { current := head for current != nil { if current.value == target { return true } current = current.next } return false } func main() { n3 := &Node{value: 3, next: nil} n2 := &Node{value: 2, next: n3} n1 := &Node{value: 1, next: n2} fmt.Println(search(n1, 2)) }
Attempts:
2 left
💡 Hint
Trace the linked list nodes and check if the target value exists.
✗ Incorrect
The search function traverses the linked list starting from the head node. It checks each node's value. Since the value 2 exists in the list, the function returns true.
❓ Predict Output
intermediateOutput when Searching a Non-Existent Value
What will be printed when searching for a value that is not in the linked list?
DSA Go
package main import "fmt" type Node struct { value int next *Node } func search(head *Node, target int) bool { current := head for current != nil { if current.value == target { return true } current = current.next } return false } func main() { n3 := &Node{value: 3, next: nil} n2 := &Node{value: 2, next: n3} n1 := &Node{value: 1, next: n2} fmt.Println(search(n1, 5)) }
Attempts:
2 left
💡 Hint
Check if the target value exists in any node.
✗ Incorrect
The value 5 is not present in the linked list, so the search function returns false.
🧠 Conceptual
advancedUnderstanding Search Time Complexity in Linked Lists
What is the time complexity of searching for a value in a singly linked list of n nodes?
Attempts:
2 left
💡 Hint
Consider how many nodes might need to be checked in the worst case.
✗ Incorrect
In the worst case, the search function may need to check every node once, resulting in linear time complexity O(n).
❓ Predict Output
advancedOutput of Search Function with Nil Head
What will be the output when the search function is called with a nil (empty) linked list?
DSA Go
package main import "fmt" type Node struct { value int next *Node } func search(head *Node, target int) bool { current := head for current != nil { if current.value == target { return true } current = current.next } return false } func main() { var head *Node = nil fmt.Println(search(head, 1)) }
Attempts:
2 left
💡 Hint
An empty list means no nodes to check.
✗ Incorrect
Since the list is empty (head is nil), the search function immediately returns false.
🔧 Debug
expertIdentify the Bug in the Search Function
Consider this modified search function. What error will it cause when searching a linked list?
DSA Go
func search(head *Node, target int) bool { current := head for current.next != nil { if current.value == target { return true } current = current.next } return false }
Attempts:
2 left
💡 Hint
Check the loop condition and what happens if head is nil.
✗ Incorrect
The loop condition current.next != nil assumes current is not nil. If head is nil, accessing current.next causes a runtime panic.
