Search a Rotated Sorted Array
Write the code, see it run, pass the checks.
The problem
- medium
- Code
Write searchRotated(nums, target): nums was sorted in ascending order with no repeats, then rotated, so a run from its start was moved to its end; it may not be rotated at all. Return the index of target, or -1 if it isn't there. Don't change nums. For example, searchRotated([4, 5, 6, 7, 0, 1, 2], 0) is 4, and searchRotated([4, 5, 6, 7, 0, 1, 2], 3) is -1. The checks run 100,000 searches on a million numbers, so looking through them one by one, even with indexOf, is too slow.
Your code must pass
- Finds numbers on either side of the rotation
- Returns -1 for missing numbers
- Works when the array isn't rotated
- Handles short arrays
- Is fast enough for 100,000 searches