Recognize that the sorted array and O(log n) requirement indicate binary search. Explain the algorithm step-by-step, emphasizing how you maintain the search space and update pointers. Then, discuss edge cases and complexity.
Pro tip: Mention that you'd use mid = left + (right - left) // 2 to avoid integer overflow, and clarify whether to return any occurrence or the first/last occurrence if duplicates exist.
Confirm assumptions: array is sorted ascending, may contain duplicates, and we need to return any index of the target or -1 if not found. Ask if the array can be empty or if there are constraints on values.
Explain that binary search is optimal for sorted arrays with O(log n) time. Initialize left and right pointers to the start and end of the array.
While left <= right, compute mid, compare array[mid] with target. If equal, return mid; if array[mid] < target, move left to mid+1; else move right to mid-1.
If the loop ends without finding the target, return -1. Discuss edge cases: empty array, single element, target smaller than first element, target larger than last element.
State time complexity O(log n) and space O(1). Walk through a small example to verify correctness, and mention potential pitfalls like integer overflow in mid calculation.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.