← 返回 bytedance 的题目列表Search in Rotated Sorted Array
类型:qbank
LeetCode 33: search for a target in a distinct, ascending array rotated at an unknown pivot, returning its index or `-1` in logarithmic time.
Requirements
The input array contains distinct integers that were originally sorted in ascending order and then rotated at an unknown pivot.
Given a target value, return its index if present; otherwise return -1.
The expected running time is O(log n).
Examples
Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4
Input: nums = [4,5,6,7,0,1,2], target = 3
Output: -1
Notes
This appeared as the second coding prompt in a third-round interview after a broad technical oral drill.
A small implementation error prevented a final result, but the interviewer acknowledged that the search logic was understood.
Preparation
Write the modified binary search from memory and state which half is sorted before deciding where the target can lie.
Drill the one-element array, no-rotation case, pivot at either boundary, and absent targets.
Dry-run the branch inequalities carefully; most bugs come from inconsistent inclusive boundaries.