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 using binary search
What is the output of the following code that counts occurrences of 3 in a sorted array?
DSA Javascript
function countOccurrences(arr, x) {
function firstOccurrence() {
let low = 0, high = arr.length - 1, result = -1;
while (low <= high) {
let mid = Math.floor((low + high) / 2);
if (arr[mid] === x) {
result = mid;
high = mid - 1;
} else if (arr[mid] < x) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return result;
}
function lastOccurrence() {
let low = 0, high = arr.length - 1, result = -1;
while (low <= high) {
let mid = Math.floor((low + high) / 2);
if (arr[mid] === x) {
result = mid;
low = mid + 1;
} else if (arr[mid] < x) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return result;
}
let first = firstOccurrence();
if (first === -1) return 0;
let last = lastOccurrence();
return last - first + 1;
}
console.log(countOccurrences([1,2,3,3,3,4,5], 3));Attempts:
2 left
💡 Hint
Think about how binary search finds the first and last positions of the target.
✗ Incorrect
The code finds the first and last index of the element 3 in the sorted array. The element 3 appears at indices 2, 3, and 4, so the count is 3.
❓ Predict Output
intermediate2:00remaining
Output when element is not present
What is the output of the code when counting occurrences of 6 in the sorted array?
DSA Javascript
function countOccurrences(arr, x) {
function firstOccurrence() {
let low = 0, high = arr.length - 1, result = -1;
while (low <= high) {
let mid = Math.floor((low + high) / 2);
if (arr[mid] === x) {
result = mid;
high = mid - 1;
} else if (arr[mid] < x) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return result;
}
function lastOccurrence() {
let low = 0, high = arr.length - 1, result = -1;
while (low <= high) {
let mid = Math.floor((low + high) / 2);
if (arr[mid] === x) {
result = mid;
low = mid + 1;
} else if (arr[mid] < x) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return result;
}
let first = firstOccurrence();
if (first === -1) return 0;
let last = lastOccurrence();
return last - first + 1;
}
console.log(countOccurrences([1,2,3,3,3,4,5], 6));Attempts:
2 left
💡 Hint
If the element is not found, the function returns 0.
✗ Incorrect
Since 6 is not in the array, the firstOccurrence function returns -1, so the countOccurrences function returns 0.
🔧 Debug
advanced2:00remaining
Identify the error in counting occurrences code
What error does the following code produce when counting occurrences of 3 in the array?
DSA Javascript
function countOccurrences(arr, x) {
function firstOccurrence() {
let low = 0, high = arr.length - 1, result = -1
while (low <= high) {
let mid = Math.floor((low + high) / 2)
if (arr[mid] === x) {
result = mid
high = mid - 1
} else if (arr[mid] < x) {
low = mid + 1
} else {
high = mid - 1
}
}
return result
}
function lastOccurrence() {
let low = 0, high = arr.length - 1, result = -1
while (low <= high) {
let mid = Math.floor((low + high) / 2)
if (arr[mid] === x) {
result = mid
low = mid + 1
} else if (arr[mid] < x) {
low = mid + 1
} else {
high = mid - 1
}
}
return result
}
let first = firstOccurrence()
if (first === -1) return 0
let last = lastOccurrence()
return last - first + 1
}
console.log(countOccurrences([1,2,3,3,3,4,5], 3))Attempts:
2 left
💡 Hint
Check how the pointers low and high are updated inside the loops.
✗ Incorrect
The code incorrectly updates high = mid - 1 and low = mid + 1 in the correct version, but in this buggy code, it uses high = mid + 1 and low = mid - 1, which can cause the loop to never terminate, resulting in an infinite loop.
❓ Predict Output
advanced2:00remaining
Output of counting occurrences with duplicates at edges
What is the output of counting occurrences of 2 in the array with duplicates at the edges?
DSA Javascript
function countOccurrences(arr, x) {
function firstOccurrence() {
let low = 0, high = arr.length - 1, result = -1;
while (low <= high) {
let mid = Math.floor((low + high) / 2);
if (arr[mid] === x) {
result = mid;
high = mid - 1;
} else if (arr[mid] < x) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return result;
}
function lastOccurrence() {
let low = 0, high = arr.length - 1, result = -1;
while (low <= high) {
let mid = Math.floor((low + high) / 2);
if (arr[mid] === x) {
result = mid;
low = mid + 1;
} else if (arr[mid] < x) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return result;
}
let first = firstOccurrence();
if (first === -1) return 0;
let last = lastOccurrence();
return last - first + 1;
}
console.log(countOccurrences([2,2,2,3,4,5,6], 2));Attempts:
2 left
💡 Hint
Duplicates at the start should be counted correctly.
✗ Incorrect
The element 2 appears three times at the start of the array, so the count is 3.
🚀 Application
expert2:00remaining
Count occurrences in a large sorted array efficiently
You have a sorted array of 1 million integers with many duplicates. Which approach is the most efficient to count occurrences of a given element x?
Attempts:
2 left
💡 Hint
Think about the time complexity of searching in a sorted array.
✗ Incorrect
Binary search finds the first and last occurrence in O(log n) time, which is much faster than scanning or building hash maps for large arrays.