Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Binary Search on Rotated Array

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Binary Search on Rotated Array. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Grab's mobile systems may receive sorted identifier data that has been rotated when batches are partitioned or refreshed. Given a rotated sorted array of distinct integers, return the index of a target value using binary search.

The array was originally sorted in strictly increasing order, then rotated at an unknown position. Return -1 if the target is not present.

Formal Specification

Implement search(nums, target).

  • Input: nums, a list of distinct integers, and target, an integer.
  • Output: The zero-based index of target in nums, or -1 when it is absent.
  • The input array must not be modified.

Constraints

  • 0 <= nums.length <= 5000
  • -10^4 <= nums[i], target <= 10^4
  • All values in nums are distinct
  • nums is formed by rotating a strictly increasing array zero or more positions

Function Signature

def search(nums, target):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output