Back to Practice
#0259

Search in Rotated Sorted Array

MediumDSA28 min20 XP

Problem

Given a rotated sorted array with distinct values, return the index of target or -1 if it is missing.

Why This Matters

Rotated search teaches the most important binary-search habit: identify which half is definitely sorted before discarding it.

Function Signature

def search_rotated(nums, target):

Examples

Example 1
Inputnums = [4,5,6,7,0,1,2], target = 0
Output4

0 is at index 4 after rotation.

Constraints

  • Return the exact requested value.
  • Handle edge cases cleanly.
  • Use the intended DSA pattern when brute force would scale poorly.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Found after pivot
Input[4,5,6,7,0,1,2], target = 0
Expected4

Binary search keeps the half that can contain 0.

Missing target
Input[4,5,6,7,0,1,2], target = 3
Expected-1

3 is not present.

Hidden Test Categories
single elementnot rotatedtarget in left sorted half