0
0
DSA Goprogramming~10 mins

Linear Search Algorithm in DSA Go - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to return the index of the target element in the slice.

DSA Go
func linearSearch(arr []int, target int) int {
    for i := 0; i < len(arr); i++ {
        if arr[i] == [1] {
            return i
        }
    }
    return -1
}
Drag options to blanks, or click blank then click option'
Atarget
Blen(arr)
Carr
Di
Attempts:
3 left
💡 Hint
Common Mistakes
Using the loop index 'i' instead of the target value for comparison.
Comparing with the entire array instead of a single element.
2fill in blank
medium

Complete the code to return -1 if the target is not found after the loop.

DSA Go
func linearSearch(arr []int, target int) int {
    for i := 0; i < len(arr); i++ {
        if arr[i] == target {
            return i
        }
    }
    return [1]
}
Drag options to blanks, or click blank then click option'
A-1
B0
Clen(arr)
Di
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 0 which could be a valid index.
Returning the length of the array which is out of bounds.
3fill in blank
hard

Fix the error in the loop condition to avoid out-of-range errors.

DSA Go
func linearSearch(arr []int, target int) int {
    for i := 0; i <= [1]; i++ {
        if arr[i] == target {
            return i
        }
    }
    return -1
}
Drag options to blanks, or click blank then click option'
Alen(arr)
Bi
Clen(arr) - 1
Dtarget
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= len(arr) causes index out of range error.
Using 'i' or 'target' as loop boundary is incorrect.
4fill in blank
hard

Fill both blanks to create a linear search that returns true if target is found, false otherwise.

DSA Go
func contains(arr []int, target int) bool {
    for [1] := 0; [1] < len(arr); [1]++ {
        if arr[[1]] == [2] {
            return true
        }
    }
    return false
}
Drag options to blanks, or click blank then click option'
Ai
Btarget
Cj
Dlen(arr)
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'j' instead of 'i' inconsistently.
Comparing with 'len(arr)' instead of 'target'.
5fill in blank
hard

Fill in the blanks to create a linear search that returns the index of the first occurrence of target or -1 if not found.

DSA Go
func findIndex(arr []int, [1] int) int {
    for [2] := 0; [2] < len(arr); [2]++ {
        if arr[[2]] == [1] {
            return [2]
        }
    }
    return -1
}
Drag options to blanks, or click blank then click option'
Atarget
Bi
Cindex
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Returning the wrong variable as index.