Search in Rotated Sorted Array
Given the array `nums` after the possible rotation and an integer `target`, return the index of `target` if it is in `nums`, or `-1` if it is not in `nums`. You must write an algorithm with `O(log n)` runtime complexity. **Examples:** ```typescript search([4, 5, 6, 7, 0, 1, 2], 0) // 4 search([4, 5, 6, 7, 0, 1, 2], 3) // -1 search([1], 0) // -1 ```
Solve It in the BrowserPractice Explaining It Out Loud
Optimal Approach
- Time complexity: O(log n)
- Space complexity: O(1)
Modified binary search halves the search range in each iteration.
Follow-Up Questions Interviewers Ask About Search in Rotated Sorted Array
- How do you determine which half is sorted?
Compare nums[left] with nums[mid]: if nums[left] <= nums[mid], the left half is normally sorted; otherwise the right half is sorted.
Practice the full problem with execution, tests, and AI code review in the interactive lab — then rehearse explaining your solution in a live voice mock interview.