0
0
DSA Goprogramming~20 mins

Count Occurrences of Element in Sorted Array in DSA Go - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Count Occurrences Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of counting occurrences in a sorted array
What is the output of the following Go code that counts occurrences of the number 3 in a sorted array?
DSA Go
package main
import "fmt"

func countOccurrences(arr []int, x int) int {
    left, right := 0, len(arr)-1
    first, last := -1, -1

    // Find first occurrence
    for left <= right {
        mid := (left + right) / 2
        if arr[mid] == x {
            first = mid
            right = mid - 1
        } else if arr[mid] < x {
            left = mid + 1
        } else {
            right = mid - 1
        }
    }

    if first == -1 {
        return 0
    }

    left, right = 0, len(arr)-1
    // Find last occurrence
    for left <= right {
        mid := (left + right) / 2
        if arr[mid] == x {
            last = mid
            left = mid + 1
        } else if arr[mid] < x {
            left = mid + 1
        } else {
            right = mid - 1
        }
    }

    return last - first + 1
}

func main() {
    arr := []int{1, 2, 3, 3, 3, 4, 5}
    fmt.Println(countOccurrences(arr, 3))
}
A4
B2
C0
D3
Attempts:
2 left
💡 Hint
Count how many times 3 appears consecutively in the sorted array.
🧠 Conceptual
intermediate
1:30remaining
Understanding the binary search approach for counting occurrences
Why does the algorithm use two binary searches to count occurrences of an element in a sorted array?
ATo find the first and last positions of the element, then calculate the count from these indices.
BTo sort the array twice before counting the element.
CTo count the element by checking every element twice.
DTo find the middle element twice and sum their values.
Attempts:
2 left
💡 Hint
Think about how to find the range of the element's positions efficiently.
🔧 Debug
advanced
2:00remaining
Identify the error in counting occurrences code
What error will this Go code produce when counting occurrences of 3 in the array?
DSA Go
package main
import "fmt"

func countOccurrences(arr []int, x int) int {
    left, right := 0, len(arr)-1
    first := -1

    // Find first occurrence
    for left <= right {
        mid := (left + right) / 2
        if arr[mid] == x {
            first = mid
            left = mid + 1 // Incorrect update
        } else if arr[mid] < x {
            left = mid + 1
        } else {
            right = mid - 1
        }
    }

    if first == -1 {
        return 0
    }

    return 1
}

func main() {
    arr := []int{1, 2, 3, 3, 3, 4, 5}
    fmt.Println(countOccurrences(arr, 3))
}
AThe code returns 0 even though 3 is present.
BThe code causes a runtime panic due to index out of range.
CThe code returns 1 instead of the correct count of 3.
DThe code enters an infinite loop.
Attempts:
2 left
💡 Hint
Check how the first occurrence is found and what the function returns.
Predict Output
advanced
1:30remaining
Output of counting occurrences for element not in array
What is the output of this Go code when counting occurrences of 6 in the sorted array?
DSA Go
package main
import "fmt"

func countOccurrences(arr []int, x int) int {
    left, right := 0, len(arr)-1
    first, last := -1, -1

    for left <= right {
        mid := (left + right) / 2
        if arr[mid] == x {
            first = mid
            right = mid - 1
        } else if arr[mid] < x {
            left = mid + 1
        } else {
            right = mid - 1
        }
    }

    if first == -1 {
        return 0
    }

    left, right = 0, len(arr)-1
    for left <= right {
        mid := (left + right) / 2
        if arr[mid] == x {
            last = mid
            left = mid + 1
        } else if arr[mid] < x {
            left = mid + 1
        } else {
            right = mid - 1
        }
    }

    return last - first + 1
}

func main() {
    arr := []int{1, 2, 3, 3, 3, 4, 5}
    fmt.Println(countOccurrences(arr, 6))
}
A-1
B0
C1
D5
Attempts:
2 left
💡 Hint
Check what happens when the element is not found in the array.
🧠 Conceptual
expert
1:00remaining
Time complexity of counting occurrences in a sorted array
What is the time complexity of counting occurrences of an element in a sorted array using the two binary searches approach?
AO(log n)
BO(n)
CO(n log n)
DO(1)
Attempts:
2 left
💡 Hint
Consider how many times the array is divided during binary search.