Skip to interview prep
Apexed

67. First and Last Position in a Sorted Array

MediumSearching

Given a sorted array that may contain duplicates, return the first and last index of target, or [-1, -1]. Use O(log n).

Example 1

Input: nums = [5, 7, 7, 8, 8, 10], target = 8
Output: [3, 4]

Constraints

  • 0 ≤ nums.length ≤ 10⁵

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 4 hidden and sample tests.

Code runs only in your browser in an isolated sandbox. Progress and submissions are saved on this device.

First and Last Position in a Sorted Array — coding interview practice — Apexed