0
0
DSA Goprogramming~20 mins

Linear Search Algorithm in DSA Go - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Linear Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
}
A1
B3
C2
D-1
Attempts:
2 left
💡 Hint
Remember that array indices start at 0 in Go.
🧠 Conceptual
intermediate
1: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?
A10
B9
C11
D1
Attempts:
2 left
💡 Hint
Think about how many elements the algorithm checks when the value is not found.
🔧 Debug
advanced
2: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))
}
Areturns -1 always
Bruntime error: index out of range
Ccompilation error: missing colon
Dreturns 0 always
Attempts:
2 left
💡 Hint
Check the loop condition carefully.
🚀 Application
advanced
2: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
A-1
B1
C3
D2
Attempts:
2 left
💡 Hint
Count nodes starting from zero at the head.
🧠 Conceptual
expert
2: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?
ABoth have O(n) time complexity because each element must be checked sequentially.
BArray search is O(log n), linked list search is O(n).
CArray search is O(1), linked list search is O(n).
DBoth have O(1) time complexity because elements are accessed directly.
Attempts:
2 left
💡 Hint
Think about how linear search works on unsorted data.