EasyArrayBinary SearchUpdated Sep 2026

35. Search Insert Position

Asked at Google, Bloomberg

Difficulty

Easy

Time

O(log n)

Space

O(1)

Pattern

Array

Companies

2

Problem

You are given a sorted array of distinct integers nums, and a target value. If target is already in the array, return its index. If it is not there, return the index where it should be inserted so the array stays sorted.

Because the array is sorted, you can find that position without looking at every element. Your algorithm must run in O(log n) time, which means binary search.

Think of the answer as the first index i where nums[i] >= target. If every element is smaller than target, the answer is nums.length (the target goes at the end).

Find the first slot that is not smaller

nums13560123target = 2: index 1 is the first place where nums[i]>= 2nums13560123target = 7: nothing is >= 7, so the insert position is4 (one past the end)
The answer is the first index where the value is >= target, or the array length if no such index exists.

Constraints

  • ●1 <= nums.length <= 10^4
  • ●-10^4 <= nums[i] <= 10^4
  • ●nums contains distinct values sorted in ascending order
  • ●-10^4 <= target <= 10^4

Examples

Example 1
Inputnums = [1,3,5,6], target = 5
Output2
Why5 is already in the array at index 2, so we return that index.
Example 2
Inputnums = [1,3,5,6], target = 2
Output1
Why2 is not present. It belongs between 1 and 3, which is index 1.
Example 3
Inputnums = [1,3,5,6], target = 7
Output4
Why7 is bigger than every element, so it would be appended at the end, index 4.

Asked At

2 companies

How to Think About It

1

Linear scan: iterate from left to right and return the first index where arr[i] >= target — O(n).

2

Binary search with explicit checks: search for exact match, then scan neighbors to find insert position.

3

Lower bound binary search: maintain the invariant that lo is the smallest possible insert position.

4

Standard binary search template with early return on exact match, adjusting lo/hi to converge on insert point.

5

Optimal lower-bound: after loop, lo always holds the correct insert index even if target is absent.

Optimal Approach

Use the standard binary search lower-bound pattern. Maintain two pointers lo and hi. In each iteration, compute mid. If arr[mid] equals target, return mid immediately. If arr[mid] is less than target, move lo to mid + 1. Otherwise, move hi to mid - 1. After the loop terminates, lo points to the correct insertion position. This works because lo always tracks the smallest index where arr[lo] >= target.

Binary search for the lower bound

nums13560123lohiStart with lo = 0 and hi = 3, target = 2nums13560123lomidhimid = 1, nums[1] = 3 > 2, so hi moves to mid - 1 = 0nums13560123lomidhimid = 0, nums[0] = 1 < 2, so lo moves to mid + 1 = 1lo = 1, hi = 0 → loop endsanswer = lo = 1
lo always marks the smallest index that could still be the answer; when lo passes hi, lo is the insert position.
ComplexityTime O(log n)Space O(1)Each step throws away half of the remaining search range, and only two index variables are kept.

What Trips People Up in Real Interviews

1

Clarify whether the array can contain duplicates — this changes whether you need lower or upper bound.

2

Confirm that O(log n) is required; interviewer will reject O(n) solutions.

3

Walk through an example where target is larger than all elements — insert at end.

4

Walk through an example where target is smaller than all elements — insert at index 0.

5

Mention that Python bisect.bisect_left directly solves this, but explain the underlying logic.

Solution Code

O(log n) time
def search_insert(nums, target):
    lo, hi = 0, len(nums) - 1
    while lo <= hi:
        mid = (lo + hi) // 2
        if nums[mid] == target:
            return mid
        elif nums[mid] < target:
            lo = mid + 1
        else:
            hi = mid - 1
    return lo

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Search Insert Position problem?

Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be inserted in order. The solution must run in O(log n) time complexity using a binary search approach.

How do you solve Search Insert Position?

The optimal approach runs in O(log n) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Search Insert Position?

Search Insert Position is asked at Google, Bloomberg. It is a easy difficulty problem.

What are common mistakes on Search Insert Position?
  • Clarify whether the array can contain duplicates — this changes whether you need lower or upper bound.
  • Confirm that O(log n) is required; interviewer will reject O(n) solutions.
  • Walk through an example where target is larger than all elements — insert at end.
  • Walk through an example where target is smaller than all elements — insert at index 0.
  • Mention that Python bisect.bisect_left directly solves this, but explain the underlying logic.