Count Target Occurrences in a Sorted Array
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Bloomberg OA reported in February 2021 hands you a sorted array and demands O(log n). That one line rules out the obvious loop. Count how many times target shows up in a nondecreasing array, and the example with [1,2,2,2,3] spells out the idea: target 2 occupies indices 1 through 3. It's a binary search problem dressed up as a counting problem. If you know lower and upper bound, it's ten minutes. If you blank under the clock, StealthCoder runs invisibly on your screen during the live OA and gives you the solution as a safety net.
The problem
Given a nondecreasing integer array nums and integer target, return how many times target occurs. Your algorithm must run in O(log n) time. Function countTarget(nums: int[], target: int) → int Examples Example 1 nums = [1,2,2,2,3] target = 2 return = 3 Target 2 occupies indices 1 through 3. Example 2 nums = [1,3,5] target = 2 return = 0 Target 2 is absent. Constraints 0 <= nums.length <= 10^5. nums is sorted in nondecreasing order.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: don't count, locate. Run binary search twice. One finds the first index where nums[i] >= target (lower bound). The other finds the first index where nums[i] > target (upper bound). The answer is upper minus lower. Empty array returns 0 naturally, and so does a missing target, since both bounds land on the same index. The common pitfall is finding one occurrence with plain binary search and then walking outward. That's O(n) on an array full of duplicates, and it breaks the stated O(log n) requirement. Other traps are off-by-one errors with hi = n versus n - 1, and infinite loops when mid is computed wrong. Use a half-open range [lo, hi) and the loop stays clean. If your head goes blank mid-assessment, StealthCoder is the hedge that keeps you from burning the clock on bound logic.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Count Target Occurrences in a Sorted Array cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Count Target Occurrences in a Sorted Array FAQ
How hard is the Bloomberg count target occurrences problem really?+
Easy to medium. The idea is short, but the boundary handling trips people up. If you've written lower bound before, you're done fast. If you haven't, expect to spend your time debugging off-by-one errors, not designing the algorithm.
What's the trick to hit O(log n)?+
Do two binary searches instead of one. Find the first index with value >= target and the first index with value > target. Subtract them. No scanning, no counters, and it handles duplicates of any length.
Why can't I just find the target and expand outward?+
Expanding is O(n) in the worst case. If the whole array is the target, you walk the whole thing. The problem explicitly requires O(log n), so that approach fails the requirement even if the outputs are correct.
What edge cases should I test?+
Test an empty array, a target smaller than every element, a target larger than every element, and a target missing in the middle like [1,3,5] with 2. Also test an array where every element equals target. All should work with the bound-subtraction approach.
How do I prepare for this in 48 hours?+
Write lower bound and upper bound from scratch three times using a half-open range. Then solve this problem by subtracting them. Practice on arrays with all duplicates and on empty input. That covers nearly every variant of this question.