Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Search Insert Position in Array

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

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

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. The input is an array of integers and a target integer; the output is a single integer index.

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