Find a Target's Index in a Rotated Sorted Array of Distinct Integers

Read the full interview experience this question came from →

Quick Overview

Find the index of a target value in an array of distinct integers that was sorted ascending and then rotated at an unknown pivot, returning -1 when it is absent. Tests adapting search to a partially ordered array in logarithmic time and handling unrotated and single-element inputs.

Find a Target's Index in a Rotated Sorted Array of Distinct Integers

Company: LinkedIn

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

An array of distinct integers was sorted in strictly increasing order and then rotated at an unknown pivot: for some `k` with `0 <= k < n`, the array `[a0, a1, ..., a(n-1)]` became `[ak, a(k+1), ..., a(n-1), a0, a1, ..., a(k-1)]`. A rotation with `k = 0` leaves the array unchanged. Given the rotated array `nums` and an integer `target`, return the index of `target` in `nums`, or `-1` if it is not present. The interviewer expects a solution whose running time is logarithmic in the length of the array. ### Function Signature ```python def search_rotated(nums: list[int], target: int) -> int: ``` ### Rules - Indices are 0-based. - All values in `nums` are distinct, so the answer is unique. - `nums` is guaranteed to be a rotation of a strictly increasing array. ### Constraints - `1 <= len(nums) <= 10^5` - `-10^9 <= nums[i] <= 10^9` - `-10^9 <= target <= 10^9` ### Examples **Example 1** - Input: `nums = [15, 18, 22, 3, 7, 11]`, `target = 7` - Output: `4` - Explanation: the array is `[3, 7, 11, 15, 18, 22]` rotated by three positions, and `7` sits at index `4`. **Example 2** - Input: `nums = [15, 18, 22, 3, 7, 11]`, `target = 20` - Output: `-1` **Example 3** - Input: `nums = [1, 4, 9, 12]`, `target = 12` - Output: `3` - Explanation: this array was not rotated (`k = 0`).

Overview: Find the index of a target value in an array of distinct integers that was sorted ascending and then rotated at an unknown pivot, returning -1 when it is absent. Tests adapting search to a partially ordered array in logarithmic time and handling unrotated and single-element inputs.

Read the full LinkedIn Software Engineer interview experience this question came from

Community answers

Answer by pjchen9413

def search_rotated(self, nums: list[int], target: int) -> int: n = len(nums) lo = 0 hi = n - 1 while lo <= hi: mid = lo + (hi - lo) // 2 if nums[mid] == target: return mid if nums[mid] < nums[hi]: if nums[mid] < target <= nums[hi]: lo = mid + 1 else: hi = mid - 1 else: if nums[lo] <= target < nums[mid]: hi = mid - 1 else: lo = mid + 1 return -1
LinkedIn logo
LinkedIn
Oct 4, 2026
mediumSoftware EngineerOtherCoding & Algorithms
1
0

An array of distinct integers was sorted in strictly increasing order and then rotated at an unknown pivot: for some k with 0 <= k < n, the array [a0, a1, ..., a(n-1)] became [ak, a(k+1), ..., a(n-1), a0, a1, ..., a(k-1)]. A rotation with k = 0 leaves the array unchanged.

Given the rotated array nums and an integer target, return the index of target in nums, or -1 if it is not present. The interviewer expects a solution whose running time is logarithmic in the length of the array.

Function Signature

def search_rotated(nums: list[int], target: int) -> int:

Rules

  • Indices are 0-based.
  • All values in nums are distinct, so the answer is unique.
  • nums is guaranteed to be a rotation of a strictly increasing array.

Constraints

  • 1 <= len(nums) <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • -10^9 <= target <= 10^9

Examples

Example 1

  • Input: nums = [15, 18, 22, 3, 7, 11] , target = 7
  • Output: 4
  • Explanation: the array is [3, 7, 11, 15, 18, 22] rotated by three positions, and 7 sits at index 4 .

Example 2

  • Input: nums = [15, 18, 22, 3, 7, 11] , target = 20
  • Output: -1

Example 3

  • Input: nums = [1, 4, 9, 12] , target = 12
  • Output: 3
  • Explanation: this array was not rotated ( k = 0 ).