0
0
DSA Goprogramming~10 mins

First and Last Occurrence of Element 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 find the first occurrence index of target in the sorted array.

DSA Go
func firstOccurrence(arr []int, target int) int {
    low, high := 0, len(arr)-1
    result := -1
    for low <= high {
        mid := low + (high - low) / 2
        if arr[mid] == [1] {
            result = mid
            high = mid - 1
        } else if arr[mid] < target {
            low = mid + 1
        } else {
            high = mid - 1
        }
    }
    return result
}
Drag options to blanks, or click blank then click option'
Ahigh
Bmid
Clow
Dtarget
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing arr[mid] with mid instead of target.
Using low or high instead of target in the comparison.
2fill in blank
medium

Complete the code to find the last occurrence index of target in the sorted array.

DSA Go
func lastOccurrence(arr []int, target int) int {
    low, high := 0, len(arr)-1
    result := -1
    for low <= high {
        mid := low + (high - low) / 2
        if arr[mid] == target {
            result = mid
            low = [1]
        } else if arr[mid] < target {
            low = mid + 1
        } else {
            high = mid - 1
        }
    }
    return result
}
Drag options to blanks, or click blank then click option'
Amid - 1
Bmid + 1
Chigh
Dlow
Attempts:
3 left
💡 Hint
Common Mistakes
Moving high instead of low after finding the target.
Setting low to mid - 1 which searches the left half incorrectly.
3fill in blank
hard

Fix the error in the code to correctly find the first occurrence of target.

DSA Go
func firstOccurrence(arr []int, target int) int {
    low, high := 0, len(arr)-1
    result := -1
    for low <= high {
        mid := low + (high - low) / 2
        if arr[mid] == target {
            result = mid
            high = [1]
        } else if arr[mid] > target {
            high = mid - 1
        } else {
            low = mid + 1
        }
    }
    return result
}
Drag options to blanks, or click blank then click option'
Amid - 1
Bmid + 1
Clow
Dhigh
Attempts:
3 left
💡 Hint
Common Mistakes
Moving high to mid + 1 which searches right half incorrectly.
Not updating high pointer after finding target.
4fill in blank
hard

Fill both blanks to create a function that returns first and last occurrence indices of target.

DSA Go
func firstAndLastOccurrence(arr []int, target int) (int, int) {
    first, last := -1, -1
    low, high := 0, len(arr)-1
    for low <= high {
        mid := low + (high - low) / 2
        if arr[mid] == target {
            first = mid
            high = [1]
        } else if arr[mid] < target {
            low = mid + 1
        } else {
            high = mid - 1
        }
    }
    low, high = 0, len(arr)-1
    for low <= high {
        mid := low + (high - low) / 2
        if arr[mid] == target {
            last = mid
            low = [2]
        } else if arr[mid] < target {
            low = mid + 1
        } else {
            high = mid - 1
        }
    }
    return first, last
}
Drag options to blanks, or click blank then click option'
Amid - 1
Bmid + 1
Chigh
Dlow
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the updates of low and high pointers.
Not resetting low and high before second search.
5fill in blank
hard

Fill all three blanks to create a map of element to its first occurrence index from the array.

DSA Go
func mapFirstOccurrences(arr []int) map[int]int {
    result := make(map[int]int)
    for i, val := range arr {
        if _, exists := result[[1]]; !exists {
            result[[2]] = [3]
        }
    }
    return result
}
Drag options to blanks, or click blank then click option'
Aval
Bi
Attempts:
3 left
💡 Hint
Common Mistakes
Using i as key instead of val.
Assigning result[i] = val which is incorrect.