← Questions

First occurrence or floor index in a sorted array

0
MicrosoftFigmaCodingLast asked Sep 2022

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 target appears in nums, 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 where target would be inserted, which is its last copy.
  • If every element is greater than target (or nums is 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
  • nums is 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 target is 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 holds target, it is the answer; otherwise the answer is the index before it, which is the last copy of the floor, or -1 if it is 0.
  • Explains why one search is enough: the insertion point already separates values below target from 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, target below every value, target above 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

binary-search · arrays

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding