📖 Scenario: You are working on a simple contact list app. Each contact is stored as a node in a linked list. You want to find if a specific contact name exists in the list.
🎯 Goal: Build a linked list of contacts and write code to search for a given contact name in the list.
📋 What You'll Learn
Create a linked list with exactly three nodes containing the names: "Alice", "Bob", and "Charlie" in that order.
Create a variable called searchName and set it to "Bob".
Write a loop to search the linked list for searchName.
Print Found if the name is in the list, otherwise print Not Found.
💡 Why This Matters
🌍 Real World
Linked lists are used in many applications like contact lists, music playlists, and undo functionality where dynamic data needs to be stored and searched efficiently.
💼 Career
Understanding linked lists and how to search them is fundamental for software developers working with data structures, memory management, and algorithm design.
Progress0 / 4 steps
1
Create the linked list nodes
Create a struct type called Node with a name field of type string and a next field of type pointer to Node. Then create three nodes named node1, node2, and node3 with names "Alice", "Bob", and "Charlie" respectively. Link them so that node1.next points to node2 and node2.next points to node3. Set node3.next to nil. Finally, create a variable head that points to node1.
DSA Go
Hint
Remember to define the Node struct first. Then create each node using &Node{...}. Link nodes by setting the next field.
2
Set the search name
Create a variable called searchName and set it to the string "Bob".
DSA Go
Hint
Use searchName := "Bob" to create the variable.
3
Search the linked list
Write a for loop that uses a variable current starting at head. Loop while current is not nil. Inside the loop, check if current.name equals searchName. If yes, set a boolean variable found to true and break the loop. If the loop ends without finding the name, found should be false. Initialize found to false before the loop.
DSA Go
Hint
Use a for loop with current := head and update current = current.next each time. Check current.name == searchName inside the loop.
4
Print the search result
Write an if statement that prints Found if found is true, otherwise prints Not Found.
DSA Go
Hint
Use if found { fmt.Println("Found") } else { fmt.Println("Not Found") } to print the result.
Practice
(1/5)
1. What is the main purpose of searching for a value in a linked list?
easy
A. To reverse the order of nodes in the list
B. To add a new node at the end of the list
C. To delete the first node of the list
D. To find if a specific value exists in the list
Solution
Step 1: Understand the search operation
Searching means checking each node to see if it contains the target value.
Step 2: Identify the goal of searching
The goal is to find whether the value exists anywhere in the linked list.
Final Answer:
To find if a specific value exists in the list -> Option D
Quick Check:
Search means find value presence = To find if a specific value exists in the list [OK]
Hint: Search means look for a value in nodes [OK]
Common Mistakes:
Confusing search with insertion or deletion
Thinking search changes the list structure
Assuming search reverses the list
2. Which of the following Go code snippets correctly checks if a linked list node's value equals the target value val?
easy
A. if current.val = val { return true }
B. if current.value == val { return true }
C. if current.value := val { return true }
D. if current.value != val { return true }
Solution
Step 1: Identify correct equality check syntax in Go
Use double equals (==) to compare values, not single equals (=) or :=.
Step 2: Check the field name and comparison logic
Assuming node field is 'value', 'current.value == val' correctly compares values.
Final Answer:
if current.value == val { return true } -> Option B
Quick Check:
Use '==' for comparison in Go [OK]
Hint: Use '==' to compare values, not '=' or ':=' [OK]
Common Mistakes:
Using '=' instead of '==' for comparison
Using ':=' inside if condition incorrectly
Checking inequality instead of equality
3. What will be the output of the following Go code that searches for value 5 in a linked list?
type Node struct {
value int
next *Node
}
func search(head *Node, val int) bool {
current := head
for current != nil {
if current.value == val {
return true
}
current = current.next
}
return false
}
// List: 1 -> 3 -> 5 -> 7 -> nil
head := &Node{1, &Node{3, &Node{5, &Node{7, nil}}}}
fmt.Println(search(head, 5))
medium
A. true
B. false
C. Compilation error
D. Runtime panic
Solution
Step 1: Trace the search function through the list
It checks nodes 1, 3, then 5. When it finds 5, it returns true immediately.
Step 2: Confirm no errors occur
Code is correct and will print true because 5 exists in the list.
Final Answer:
true -> Option A
Quick Check:
Value 5 found in list = true [OK]
Hint: Stop search when value found, else false [OK]
Common Mistakes:
Assuming search returns false if value is in list
Thinking code causes panic due to nil pointer
Confusing output with string instead of boolean
4. Identify the bug in this Go function that searches for a value in a linked list:
func search(head *Node, val int) bool {
current := head
for current.next != nil {
if current.value == val {
return true
}
current = current.next
}
return false
}
medium
A. Loop condition misses checking the last node
B. Comparison uses '=' instead of '=='
C. Function returns true even if value not found
D. Variable 'current' is not initialized
Solution
Step 1: Analyze the loop condition
The loop runs while current.next != nil, so it stops before last node.
Step 2: Understand impact on search
The last node's value is never checked, so search can miss the value if it's there.
Final Answer:
Loop condition misses checking the last node -> Option A
Quick Check:
Loop must check all nodes including last [OK]
Hint: Use 'current != nil' to check all nodes [OK]
Common Mistakes:
Using current.next != nil instead of current != nil
Forgetting to check last node's value
Misreading loop condition as correct
5. Given a linked list where nodes may contain duplicate values, how would you modify the search function to return the count of occurrences of a target value val instead of just true/false? Choose the correct approach.
hard
A. Return true immediately when first node.value == val is found
B. Use recursion to return true if any node.value == val
C. Traverse all nodes, increment count when node.value == val, return count
D. Stop traversal when node.value != val is found
Solution
Step 1: Understand requirement to count all occurrences
We must check every node because duplicates can appear anywhere.
Step 2: Modify search to count matches
Traverse all nodes, increase count each time node.value equals val, then return count.
Final Answer:
Traverse all nodes, increment count when node.value == val, return count -> Option C
Quick Check:
Count all matches by full traversal [OK]
Hint: Count matches by checking every node [OK]
Common Mistakes:
Stopping search after first match
Returning boolean instead of count
Ignoring duplicates in list
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.