Given an integer array sorted in ascending order, nums, and an integer target, search in nums for the target. Since the size of nums is unknown to you, you may only access the array using an ArrayReader interface, where ArrayReader.get(k) returns the element of the array at index k (0-indexed).
You may assume that all integers in the array are less than 10000, and if you access the array out of bounds, ArrayReader.get will return 2147483647.
Return the index of the target if it exists in the array; otherwise, return -1.
Example 1:
Input: nums = [-1,0,3,5,9,12], target = 9
Output: 4
Explanation: 9 exists in nums and its index is 4
Example 2:
Input: nums = [-1,0,3,5,9,12], target = 2
Output: -1
Explanation: 2 does not exist in nums so return -1
Constraints:
1 <= nums.length <= 104-9999 <= nums[i] <= 9999nums are unique.nums is sorted in ascending order.-9999 <= target <= 9999When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We need to find a specific value within a collection that has an unknown size. The brute force way is to simply check every possible place in the collection, one by one, until we either find the value or determine it's not there. It's like looking for a specific book on a bookshelf of unknown length by checking each book individually.
Here's how the algorithm would work step-by-step:
def search_in_unknown_size_array_brute_force(array, target):
index = 0
# Iterate through the array until an error occurs
while True:
try:
current_value = array[index]
# We found our target so return the index
if current_value == target:
return index
# If we went past the target then return -1
if current_value > target:
return -1
index += 1
# Array index out of bounds means element isn't present
except IndexError:
return -1The problem is like finding a specific page in a very large book, but you don't know how many pages are in the book. The best way is to start by guessing further and further out until we go too far, then narrow down the search.
Here's how the algorithm would work step-by-step:
def search_in_sorted_array_of_unknown_size(sorted_array, target_value):
left_index = 0
right_index = 1
# Expand the search range until target_value is within.
while sorted_array.get(right_index) is not None and sorted_array.get(right_index) < target_value:
left_index = right_index
right_index *= 2
# Array size found or target > last element.
while left_index <= right_index:
middle_index = left_index + (right_index - left_index) // 2
middle_value = sorted_array.get(middle_index)
# Key value not present.
if middle_value is None:
right_index = middle_index - 1
continue
if middle_value == target_value:
return middle_index
# Adjust search based on comparison.
if middle_value < target_value:
left_index = middle_index + 1
else:
right_index = middle_index - 1
# Target not found after binary search
return -1| Case | How to Handle |
|---|---|
| reader is null or reader.get(0) returns out-of-bounds indicator | Return -1 immediately, indicating target not found. |
| Target is smaller than the first element in the array | Return -1 as the array is sorted and target cannot exist. |
| Target is larger than all elements in the (virtually sized) array | Binary search will eventually have left > right, and return -1 after exhausting search space defined by initial exponential search. |
| Target exists at index 0 | Binary search should correctly identify index 0 during the search. |
| Array contains only one element which matches the target | The initial size estimate combined with binary search will quickly converge to the single element index if it matches the target. |
| Out-of-bounds indicator is a valid integer (e.g., INT_MAX) | Ensure we don't compare target to the out-of-bounds indicator directly in binary search to avoid unexpected results or overflow. |
| Integer overflow when calculating mid in binary search (left + right) / 2 | Use left + (right - left) / 2 to prevent integer overflow when calculating the mid-point. |
| Target is present multiple times in the sorted array | Binary search may return any valid index where the target exists, and problem doesn't require a particular index. |