Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Binary Search Insert Position

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

Your question is Binary Search Insert Position. 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

Given a sorted array of distinct integers nums and an integer target, return the index if target is found. If it is not found, return the index where it should be inserted to keep the array sorted. Implement the solution in O(log n) time.

Constraints

  • 1 <= len(nums) <= 10^4
  • -10^4 <= nums[i], target <= 10^4
  • nums is sorted in ascending order
  • All values in nums are distinct

Function Signature

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