Implement Lower Bound

easy

You are given a sorted array arr (0-based indexing, non-decreasing order — duplicates allowed) and a number target.

Return the lower bound of target in arr: the smallest index i such that arr[i] >= target.

If every element of arr is strictly smaller than target, return arr.length — the position just past the end, where target would have to be appended to keep the array sorted.

Hints

The array is sorted — that single fact should make you suspicious of any plan that checks every element.
Label each element yes or no: is it greater than or equal to target? In a sorted array the labels read NO NO NO YES YES — you are hunting the boundary between the two zones.
Binary search, but do not stop at a match: when arr[mid] >= target, keep mid as a candidate and continue left with hi = mid. Start hi at n (not n - 1) so that one-past-the-end stays a possible answer.

Common doubts

Lower bound does not require the target to be present. It returns the index where target would be inserted to keep the array sorted — the first index holding a value >= target.
Return the array length n. The target would have to be appended at the end, so its insertion point is one past the last index.
Return the first (smallest) index of the equal run. That is exactly what the >= comparison plus the hi = mid move guarantees.
Lower bound finds the first index with arr[i] >= target; upper bound finds the first index with arr[i] > target. They differ only in the comparison, and their difference counts the occurrences of target.

Interview follow-ups

Change the probe condition from arr[mid] >= target to arr[mid] > target — everything else stays identical. It returns the first index strictly greater than the target.
Run both bounds: upperBound(arr, target) - lowerBound(arr, target). Two binary searches, about 40 probes total on a million elements.
Yes — C++ has std::lower_bound, Python has bisect.bisect_left, Go has sort.SearchInts. Say so, then offer to implement it by hand; interviewers usually want the hand-rolled boundary search.

Fun facts

  • You are re-implementing a battle-tested standard-library primitive: C++'s std::lower_bound, Python's bisect_left, and Go's sort.Search all use exactly this first-YES boundary search.
  • The lo < hi / hi = mid template generalizes far beyond arrays — it powers binary search on the answer in problems like Koko Eating Bananas and Allocate Minimum Pages, where the yes/no question is a feasibility check instead of a comparison.

Asked at

AmazonMicrosoftGoogleOracleAdobe
Frequently Sometimes Occasionally
Example 1
Input: arr = [2, 3, 7, 10, 11, 11, 25], target = 9
Output: 3
Index 3 is the smallest index where the element (arr[3] = 10) is greater than or equal to 9.
Example 2
Input: arr = [2, 3, 7, 10, 11, 11, 25], target = 11
Output: 4
Index 4 holds the first 11 — the smallest index with an element greater than or equal to 11.
Example 3
Input: arr = [2, 3, 7, 10, 11, 11, 25], target = 100
Output: 7
No element is greater than or equal to 100, so the answer is the array length, 7.
Constraints

- 1 <= arr.size <= 10^6 - 1 <= arr[i] <= 10^6 - 1 <= target <= 10^6 - arr is sorted in non-decreasing order

Solve this problem →