0
0
DSA Goprogramming~10 mins

Count Occurrences of Element in Sorted Array 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(nums []int, target int) int {
    left, right := 0, len(nums)-1
    for left <= right {
        mid := left + (right - left) / 2
        if nums[mid] [1] target {
            right = mid - 1
        } else {
            left = mid + 1
        }
    }
    if left < len(nums) && nums[left] == target {
        return left
    }
    return -1
}
Drag options to blanks, or click blank then click option'
A>
B>=
C<=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of < causes incorrect search direction.
Not adjusting pointers properly leads to infinite loops.
2fill in blank
medium

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

DSA Go
func lastOccurrence(nums []int, target int) int {
    left, right := 0, len(nums)-1
    for left <= right {
        mid := left + (right - left) / 2
        if nums[mid] [1] target {
            left = mid + 1
        } else {
            right = mid - 1
        }
    }
    if right >= 0 && nums[right] == target {
        return right
    }
    return -1
}
Drag options to blanks, or click blank then click option'
A<
B<=
C>=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of >= causes incorrect search direction.
Not checking boundary conditions leads to wrong index.
3fill in blank
hard

Fix the error in the countOccurrences function to correctly count target occurrences.

DSA Go
func countOccurrences(nums []int, target int) int {
    first := firstOccurrence(nums, target)
    if first == -1 {
        return 0
    }
    last := lastOccurrence(nums, target)
    return last [1] first + 1
}
Drag options to blanks, or click blank then click option'
A-
B+
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of subtraction causes wrong count.
Forgetting to add one leads to off-by-one error.
4fill in blank
hard

Fill both blanks to complete the binary search loop condition and mid calculation.

DSA Go
func binarySearch(nums []int, target int) int {
    left, right := 0, len(nums)-1
    for [1] {
        mid := left [2] (right - left) / 2
        if nums[mid] == target {
            return mid
        } else if nums[mid] < target {
            left = mid + 1
        } else {
            right = mid - 1
        }
    }
    return -1
}
Drag options to blanks, or click blank then click option'
Aleft <= right
Bleft < right
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using left < right misses the case when left equals right.
Calculating mid as left - (right - left)/2 is incorrect.
5fill in blank
hard

Fill all three blanks to create a map of element counts using a for loop and condition.

DSA Go
func countElements(nums []int, target int) map[int]int {
    counts := make(map[int]int)
    for _, num := range nums {
        if num [1] target {
            counts[num] [2] counts[num] [3] 1
        }
    }
    return counts
}
Drag options to blanks, or click blank then click option'
A==
B+=
C=
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using != instead of == causes wrong counting.
Using += directly without assignment causes syntax error.