FastPrepSearch in Rotated Sorted Array

Search in Rotated Sorted Array

Kotak Mahindra Bank logoKotak Mahindra Bank● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

You are given an integer array nums containing distinct values. It was sorted in increasing order and then rotated at an unknown pivot.

Return the index of target if it appears in nums; otherwise return -1. Your algorithm must run in O(log n) time.

Function

searchRotated(nums: int[], target: int) → int

Examples

Example 1

nums = [4,5,6,7,0,1,2]target = 0return = 4

The target appears at index 4.

Example 2

nums = [4,5,6,7,0,1,2]target = 3return = -1

The target is absent.

Constraints

  • 1 <= nums.length <= 100000.
  • Every value in nums is distinct and in [-10^9, 10^9].
  • nums is a rotation of a strictly increasing array.
  • target is in [-10^9, 10^9].

Source note: Author-written prompt excerpt retained for source fidelity.

More Kotak Mahindra Bank problems

See Kotak Mahindra Bank hiring insights
public int searchRotated(int[] nums, int target) {
    // Write your code here.
}
nums[4,5,6,7,0,1,2]
target0
expected4
Checking account…