Count Distinct Values in a Sorted Array
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The follow-up is the whole point here. Google reported this one in October 2024: count distinct values in a nondecreasing array, then do it in O(k log n) by skipping long runs of duplicates. The basic version is a single pass comparing neighbors. The follow-up is where candidates freeze. Arrays go up to 200000 elements and can be empty, so edge cases matter too. If you've got an OA invite, expect to write both versions. StealthCoder is the safety net running invisibly during the live assessment if your mind goes blank on the binary search jump.
The problem
Given a nondecreasing integer array nums, return the number of distinct values in the array. As a follow-up, let k be the number of distinct values. Design an approach that can skip long runs of duplicates and runs in O(k log n) time. Function countDistinct(nums: int[]) → int Examples Example 1 nums = [-3,-3,-1,2,2,2,8] return = 4 The distinct values are -3, -1, 2, and 8. Example 2 nums = [] return = 0 An empty array contains no distinct values. Constraints 0 <= nums.length <= 200000 -10^9 <= nums[i] <= 10^9 nums is sorted in nondecreasing order.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Start with the linear version: count positions where nums[i] != nums[i-1], and return 0 for an empty array. That passes the base problem. For O(k log n), keep a pointer i at the start of a run. Binary search for the first index greater than nums[i], which is an upper bound. Add one to the count, set i to that index, repeat until i reaches n. Each jump costs log n and you make k jumps. The common pitfall is an off-by-one in the upper bound, or using a lower bound and looping forever on the same value. Also guard the empty input before touching nums[0]. Don't use a hash set. It ignores the sorted property and fails the follow-up. If you blank on the bisect boundary during the live OA, StealthCoder can supply the clean upper-bound loop while you stay in control of the explanation.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Count Distinct Values 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. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Google's OA.
Google reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Count Distinct Values in a Sorted Array FAQ
What's the trick in the Google distinct-values question?+
Sorted means equal values are contiguous. The base answer counts neighbor changes. The follow-up uses binary search to jump to the first element greater than the current value, so each distinct value costs one log n search instead of scanning every duplicate.
How hard is this really?+
Easy for the linear version, easy-medium for the O(k log n) follow-up. The logic is short. Most mistakes come from the upper-bound boundary, mid calculation, and the empty array case, not from the idea itself.
Do I need the O(k log n) solution or is linear enough?+
Write the linear one first so you have a correct answer. The problem explicitly asks for the follow-up, so assume it may be tested or scored. When k is much smaller than n, the jump approach is faster, especially near 200000 elements.
What edge cases should I test?+
Test the empty array returning 0, a single element, all identical values, all distinct values, and negatives like -3 and -1 from the example. Extreme values near plus or minus 10^9 are fine in most languages, but watch mid overflow in fixed-width ints.
How do I prepare for this in 48 hours?+
Write an upper-bound binary search from memory until it's automatic. Then wrap it in the jump loop and run it on the examples. Also redo the neighbor-compare version. That covers every variant of this question you're likely to see.