Challenge - 5 Problems
Count Occurrences Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateCount occurrences of 3 in a sorted array
What is the output of the following code that counts how many times 3 appears in the sorted array?
DSA Python
def count_occurrences(arr, x): return arr.count(x) arr = [1, 2, 3, 3, 3, 4, 5] print(count_occurrences(arr, 3))
Attempts:
2 left
💡 Hint
Use the list method that counts how many times an element appears.
✗ Incorrect
The number 3 appears three times in the list, so the count is 3.
❓ Predict Output
intermediateCount occurrences using binary search boundaries
What is the output of this code that uses binary search to count occurrences of 2 in a sorted array?
DSA Python
def binary_search_left(arr, x): low, high = 0, len(arr) while low < high: mid = (low + high) // 2 if arr[mid] < x: low = mid + 1 else: high = mid return low def binary_search_right(arr, x): low, high = 0, len(arr) while low < high: mid = (low + high) // 2 if arr[mid] <= x: low = mid + 1 else: high = mid return low arr = [1, 2, 2, 2, 3, 4] count = binary_search_right(arr, 2) - binary_search_left(arr, 2) print(count)
Attempts:
2 left
💡 Hint
Count is right boundary minus left boundary of the target element.
✗ Incorrect
The number 2 appears three times between the left and right binary search boundaries.
🔧 Debug
advancedFind the bug in counting occurrences with binary search
What error does this code raise when counting occurrences of 5 in the sorted array?
DSA Python
def count_occurrences(arr, x): left = 0 right = len(arr) - 1 count = 0 while left <= right: mid = (left + right) // 2 if arr[mid] == x: count += 1 left = mid + 1 elif arr[mid] < x: left = mid + 1 else: right = mid - 1 return count arr = [1, 2, 3, 4, 6, 7] print(count_occurrences(arr, 5))
Attempts:
2 left
💡 Hint
Check if the loop can get stuck when the element is not found.
✗ Incorrect
The code increments count only when arr[mid] == x but moves left pointer forward, causing the loop to never end if x is not in the array.
🧠 Conceptual
advancedWhy use binary search to count occurrences in sorted array?
Which reason best explains why binary search is efficient for counting occurrences of an element in a sorted array?
Attempts:
2 left
💡 Hint
Think about how binary search narrows down positions in a sorted list.
✗ Incorrect
Binary search quickly finds the boundaries of the target element, so counting is just subtracting indices.
🚀 Application
expertCount occurrences of multiple elements efficiently
Given a sorted array and a list of query elements, which approach efficiently counts occurrences of each query element?
Attempts:
2 left
💡 Hint
Think about repeated queries and how to avoid scanning the whole array each time.
✗ Incorrect
Using binary search for each query on a sorted array is efficient (O(Q log N)) compared to scanning (O(Q N)). Preprocessing with a dictionary is possible but not always efficient if queries are few or array is large.
