The Number of Equal Numbers Blocks problem presents an interactive problem where you have a hidden array and an API call query(l, r) that returns whether all elements in arr[l..r] are equal. Find all maximal blocks of equal consecutive elements with the minimum number of API calls. This coding problem uses binary search to find block boundaries efficiently.
Google asks this interactive binary search problem to test the ability to design efficient API-usage strategies. The naive O(n) sequential scan uses n-1 calls; binary search reduces calls per block boundary to O(log n). The array, interactive, and binary search interview pattern is directly demonstrated.
Binary search for block boundaries. Start at index 0. For each current block starting at left: binary search for the rightmost index where query(left, mid) is true. This gives the right boundary of the current block. Record the block, then move left to right + 1. Continue until the end of the array. Each block boundary discovery takes O(log n) API calls.
Array: [3, 3, 3, 7, 7, 1, 1, 1, 1]. Blocks: [3,3,3], [7,7], [1,1,1,1].
left correctly after finding each block boundary.Interactive binary search problems require designing the binary search condition around the API. Here: "find the rightmost index r such that all elements from left to r are equal" is a monotonic condition (if (left,r) is uniform, so is (left,r-1)). Standard binary search applies. Practice interactive problems by mentally simulating API calls — always aim to minimize total calls by finding block boundaries in O(log n) per block.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Search in a Sorted Array of Unknown Size | Medium | Solve | |
| Find the Index of the Large Integer | Medium | Solve | |
| Find in Mountain Array | Hard | Solve | |
| Maximum Font to Fit a Sentence in a Screen | Medium | Solve | |
| Leftmost Column with at Least a One | Medium | Solve |