Challenge - 5 Problems
Linear Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Linear Search on an Array
What is the output of this Go code that performs a linear search for the value 7 in the array?
DSA Go
package main import "fmt" func linearSearch(arr []int, target int) int { for i, v := range arr { if v == target { return i } } return -1 } func main() { arr := []int{3, 5, 7, 9, 11} index := linearSearch(arr, 7) fmt.Println(index) }
Attempts:
2 left
💡 Hint
Remember that array indices start at 0 in Go.
✗ Incorrect
The value 7 is at index 2 in the array [3, 5, 7, 9, 11]. The linear search returns the index where the target is found.
🧠 Conceptual
intermediate1:30remaining
Number of Comparisons in Worst Case
If you perform a linear search for a value that is not in an array of length 10, how many comparisons will the algorithm make in the worst case?
Attempts:
2 left
💡 Hint
Think about how many elements the algorithm checks when the value is not found.
✗ Incorrect
In the worst case, linear search checks every element once, so it makes 10 comparisons for an array of length 10.
🔧 Debug
advanced2:00remaining
Find the Error in Linear Search Implementation
What error will this Go code cause when running the linear search function?
DSA Go
package main import "fmt" func linearSearch(arr []int, target int) int { for i := 0; i < len(arr); i++ { if arr[i] == target { return i } } return -1 } func main() { arr := []int{1, 2, 3} fmt.Println(linearSearch(arr, 2)) }
Attempts:
2 left
💡 Hint
Check the loop condition carefully.
✗ Incorrect
The loop uses i <= len(arr), which tries to access arr[len(arr)] causing an index out of range runtime error.
🚀 Application
advanced2:30remaining
Linear Search on a Linked List
Given a singly linked list with nodes containing values [4, 8, 15, 16, 23], what is the output of searching for value 15 using linear search?
DSA Go
type Node struct { value int next *Node } func linearSearch(head *Node, target int) int { index := 0 current := head for current != nil { if current.value == target { return index } current = current.next index++ } return -1 } // List: 4 -> 8 -> 15 -> 16 -> 23 -> nil // Search for 15
Attempts:
2 left
💡 Hint
Count nodes starting from zero at the head.
✗ Incorrect
The value 15 is at the third node, which is index 2 starting from 0.
🧠 Conceptual
expert2:00remaining
Time Complexity of Linear Search in Different Data Structures
Which statement correctly describes the time complexity of linear search for an element in an unsorted array and an unsorted linked list, both of size n?
Attempts:
2 left
💡 Hint
Think about how linear search works on unsorted data.
✗ Incorrect
Linear search checks each element one by one in both arrays and linked lists, so both have O(n) time complexity.