Count uniques in sparse sorted array
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: This question evaluates proficiency in algorithm design and complexity analysis, focusing on reasoning about how a sorted array with very few distinct values can be handled to count unique elements.
Constraints
- 0 <= n <= 200000
- -10^9 <= nums[i] <= 10^9
- nums is sorted in non-decreasing order
- Aim for O(k log n) time where k is the number of distinct values
- Use O(1) extra space
Hints
- In a sorted array, equal values form contiguous blocks.
- Use binary search (upper bound) to find the index after the last occurrence of the current value.
- Jump to that index and repeat until you reach the end.
- In Python, bisect_right can be used to find the upper bound.