Search a Rotated Sorted Array
Updated
Problem statement
An array of different integers was sorted in increasing order and then rotated: its first k elements, for some k with 0 <= k < n, were moved in the same order to the end. For example, rotating [3,9,17,25,31,40,52] with k = 4 gives [31,40,52,3,9,17,25]. With k = 0 the array stays sorted.
You are given the rotated array nums (but not k) and a value target. Return the index of target in nums, or -1 if it is not in the array.
Function
searchRotated(nums: int[], target: int) → intExamples
Example 1
nums = [31,40,52,3,9,17,25]target = 9return = 4The sorted array [3,9,17,25,31,40,52] was rotated so that it starts at 31. The value 9 sits at index 4.
Example 2
nums = [12,18,2,5]target = 13return = -113 is between 12 and 18 in value, but it is not in the array, so the answer is -1.
Example 3
nums = [-6,-1,4,10]target = 10return = 3The array is not rotated at all (rotation by 0). 10 is the last element, index 3.
Constraints
1 <= nums.length <= 5000-10^9 <= nums[i], target <= 10^9- All values in
numsare different, andnumsis an increasing array rotated by somekwith0 <= k < nums.length
More Adobe Phone Screen questions
- Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
- All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
- Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
- Binary Tree Level-Order Traversal by LevelsPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
- Count Occurrences in a Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
- Count Pairs with Absolute Difference KPHONE SCREEN · Seen Jul 2026
- Count Subsets with Sum KPHONE SCREEN · Seen Jul 2026
- Numbers with Exactly Three DivisorsPHONE SCREEN · Seen Jul 2026
- Sum of Even Nodes on a Path in a Numbered Binary TreePHONE SCREEN · Seen Jul 2026
- Largest Value in Every Sliding WindowPHONE SCREEN · Seen Jul 2026
Similar questions at other companies
- Longest Increasing SubsequenceByteDance · PHONE SCREENByteDance interview questions
- Koko Eating BananasThe D.E. Shaw Group · PHONE SCREENThe D.E. Shaw Group interview questions
- Find First and Last Position in a Sorted ArrayInstacart · PHONE SCREENInstacart interview questions
- Random Pick with Weight Using Recorded DrawsGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Longest Increasing SubsequenceAngel One · PHONE SCREEN
- Aggressive CowsAmazon · PHONE SCREEN
- Find First and Last Position of Element in Sorted ArrayEPAM · PHONE SCREENEPAM interview questions
- Find K Closest Elements in a Sorted ArrayNavi · PHONE SCREEN
- Search in Rotated Sorted ArrayZoomInfo · PHONE SCREEN
- Find a Peak ElementFox Corporation · PHONE SCREEN