2dbi
Home/Splunk/Find First and Last Position in Sorted Array
SSplunk·DSAEngineerTechnical Phone Screen

Find First and Last Position in Sorted Array

Problem

Find the first and last index of a target in a sorted array; the interviewer specifically wants a binary-search solution.

Example

[5,7,7,8,8,10], target=8 -> [3,4]

Constraints

  • 0 ≤ n ≤ 10^5

Approach

Two binary searches (lower/upper bound). Reported Splunk question.

added 6 days ago
LeadersAccount