Challenge - 5 Problems
Count Occurrences Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)) }
Attempts:
2 left
💡 Hint
Count how many times 3 appears consecutively in the sorted array.
✗ Incorrect
The number 3 appears three times consecutively at indices 2, 3, and 4. The code finds the first and last occurrence indices and returns their difference plus one.
🧠 Conceptual
intermediate1: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?
Attempts:
2 left
💡 Hint
Think about how to find the range of the element's positions efficiently.
✗ Incorrect
The two binary searches locate the first and last occurrence of the element, allowing calculation of total occurrences by subtracting indices.
🔧 Debug
advanced2: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)) }
Attempts:
2 left
💡 Hint
Check how the first occurrence is found and what the function returns.
✗ Incorrect
The code incorrectly updates 'left' when finding the first occurrence, causing it to find the last occurrence instead. Also, it returns 1 instead of the count.
❓ Predict Output
advanced1: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)) }
Attempts:
2 left
💡 Hint
Check what happens when the element is not found in the array.
✗ Incorrect
The element 6 is not in the array, so the first occurrence index remains -1 and the function returns 0.
🧠 Conceptual
expert1: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?
Attempts:
2 left
💡 Hint
Consider how many times the array is divided during binary search.
✗ Incorrect
Each binary search runs in O(log n) time, and since two binary searches are performed, the total time complexity remains O(log n).