First occurrence or floor index in a sorted array
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Figma's coding round and at Microsoft, last asked Sep 2022. It tests binary-search and arrays.
You are given an array nums of integers sorted in non-decreasing order. It may contain repeated values. You are also given an integer target.
Implement search_first_or_floor(nums, target) -> int:
- If
targetappears innums, return the index of its first occurrence. - Otherwise, return the index of the closest element that is less than or equal to
target(its floor). If that value repeats, return the copy nearest to wheretargetwould be inserted, which is its last copy. - If every element is greater than
target(ornumsis empty), return-1.
Your solution should run in O(log n) time.
Constraints
0 <= len(nums) <= 10^5-10^9 <= nums[i], target <= 10^9-
numsis sorted in non-decreasing order.
Example 1
nums = [4, 6, 6, 6, 11], target = 6
Output: 1
6 appears at indices 1, 2 and 3, so the first occurrence is index 1.
Example 2
nums = [4, 6, 6, 6, 11], target = 9
Output: 3
9 is absent. The largest value not above it is 6, and its copy nearest the insertion point is at index 3. For target = 2 the answer would be -1.
Clarifying questions
- Is an empty array possible, and should it return
-1? - When
targetis absent and the floor value repeats, is the last copy of it the one to return, as in Example 2? - Should the solution use the standard library (
bisect), or write the binary search by hand? - Are there any very large arrays where recursion depth would matter?
What a strong answer covers
- One lower-bound binary search for the first index with
nums[i] >= target. If that index holdstarget, it is the answer; otherwise the answer is the index before it, which is the last copy of the floor, or-1if it is 0. - Explains why one search is enough: the insertion point already separates values below
targetfrom the rest. - Writes the loop with a clear invariant (half-open
[lo, hi)), so there are no off-by-one errors or infinite loops. O(log n) time, O(1) space. - Edge cases: an empty array,
targetbelow every value,targetabove every value (the last index), all values equal, and a single element. - Tests: both examples, the edge cases, and a comparison with a linear scan on random sorted arrays with many duplicates.
Follow-ups
- Return the first and last index of
target(its range). - Return the ceiling instead (the smallest value at least
target). - The array is sorted but rotated.
- The array is too large to fit in memory, or is only reachable through a
get(i)call with unknown length.
Where it's asked
- Figma · Coding · Last asked Sep 202260 min · One or two pair-programming rounds against provided tests
- Microsoft
What it tests
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.

