0
0
DSA Goprogramming~20 mins

First and Last Occurrence of Element in DSA Go - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
First and Last Occurrence Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Find first and last occurrence indexes in a sorted array
What is the output of the following Go code that finds the first and last occurrence of the number 3 in a sorted array?
DSA Go
package main
import "fmt"

func firstAndLast(arr []int, target int) (int, int) {
    first, last := -1, -1
    for i, v := range arr {
        if v == target {
            if first == -1 {
                first = i
            }
            last = i
        }
    }
    return first, last
}

func main() {
    arr := []int{1, 2, 3, 3, 3, 4, 5}
    f, l := firstAndLast(arr, 3)
    fmt.Printf("%d %d\n", f, l)
}
A2 4
B1 3
C3 5
D-1 -1
Attempts:
2 left
💡 Hint
Look for the first and last index where the target appears in the array.
Predict Output
intermediate
2:00remaining
Output of binary search for first occurrence
What is the output of this Go code that uses binary search to find the first occurrence of 5 in a sorted array?
DSA Go
package main
import "fmt"

func firstOccurrence(arr []int, target int) int {
    low, high := 0, len(arr)-1
    result := -1
    for low <= high {
        mid := (low + high) / 2
        if arr[mid] == target {
            result = mid
            high = mid - 1
        } else if arr[mid] < target {
            low = mid + 1
        } else {
            high = mid - 1
        }
    }
    return result
}

func main() {
    arr := []int{1, 3, 5, 5, 5, 7, 9}
    fmt.Println(firstOccurrence(arr, 5))
}
A3
B4
C2
D-1
Attempts:
2 left
💡 Hint
Binary search moves left when it finds the target to find the first occurrence.
🔧 Debug
advanced
2:00remaining
Identify the error in last occurrence binary search
What error does the following Go code produce when trying to find the last occurrence of 4 in the array?
DSA Go
package main
import "fmt"

func lastOccurrence(arr []int, target int) int {
    low, high := 0, len(arr)-1
    result := -1
    for low <= high {
        mid := (low + high) / 2
        if arr[mid] == target {
            result = mid
            low = mid + 1
        } else if arr[mid] > target {
            high = mid - 1
        } else {
            low = mid + 1
        }
    }
    return result
}

func main() {
    arr := []int{1, 2, 4, 4, 4, 5, 6}
    fmt.Println(lastOccurrence(arr, 4))
}
ANo error, output is 4
BIndex out of range runtime error
CCompilation error
DInfinite loop
Attempts:
2 left
💡 Hint
Check the loop conditions and index updates carefully.
Predict Output
advanced
2:00remaining
Output of combined first and last occurrence function
What is the output of this Go program that finds first and last occurrence of 7 in a sorted array?
DSA Go
package main
import "fmt"

func firstAndLastOccurrence(arr []int, target int) (int, int) {
    first, last := -1, -1
    low, high := 0, len(arr)-1
    // Find first occurrence
    for low <= high {
        mid := (low + high) / 2
        if arr[mid] == target {
            first = mid
            high = mid - 1
        } else if arr[mid] < target {
            low = mid + 1
        } else {
            high = mid - 1
        }
    }
    low, high = 0, len(arr)-1
    // Find last occurrence
    for low <= high {
        mid := (low + high) / 2
        if arr[mid] == target {
            last = mid
            low = mid + 1
        } else if arr[mid] > target {
            high = mid - 1
        } else {
            low = mid + 1
        }
    }
    return first, last
}

func main() {
    arr := []int{1, 3, 5, 7, 7, 7, 9, 11}
    f, l := firstAndLastOccurrence(arr, 7)
    fmt.Printf("%d %d\n", f, l)
}
A4 6
B2 4
C-1 -1
D3 5
Attempts:
2 left
💡 Hint
Binary search finds first and last indexes separately.
🧠 Conceptual
expert
1:30remaining
Time complexity of first and last occurrence search
What is the time complexity of finding the first and last occurrence of an element in a sorted array using binary search?
AO(n)
BO(log n)
CO(n log n)
DO(1)
Attempts:
1 left
💡 Hint
Binary search divides the search space in half each step.