My first instinct was linear scan, which, yeah, immediately wrong given the constraint.
Use binary search twice: first to find the leftmost occurrence of the target, then to find the rightmost occurrence. Modify the standard binary search to continue searching even after finding the target, adjusting the search range based on whether you're looking for the first or last position. This ensures O(log n) time complexity.
Pro tip: Clarify with the interviewer whether you can use built-in functions like lower_bound/upper_bound, but be prepared to implement them manually. Also, discuss edge cases like empty array or target not present to show thoroughness.
Confirm the array is sorted, may contain duplicates, and the target may not exist. Discuss handling of empty array and single-element array.
Modify binary search to find the leftmost index: when nums[mid] == target, record mid and move the right pointer to mid-1 to search left half.
Similarly, find the rightmost index: when nums[mid] == target, record mid and move the left pointer to mid+1 to search right half.
Write code for both searches, ensuring they run in O(log n). Return [-1, -1] if either search fails to find the target.
Walk through test cases: target present multiple times, target absent, empty array, target at boundaries. Verify time complexity.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.