Do this now: read constraints, then see target duplicates highlighted.
Example Visual
5[0]
7[1]
7[2]
8[3]
8[4]
10[5]
Target = 8 appears at indices 3 and 4.
Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value.

If target is not found in the array, return [-1, -1].

You must write an algorithm with O(log n) runtime complexity.

Example 1:
Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]
Example 2:
Input: nums = [5,7,7,8,8,10], target = 6
Output: [-1,-1]
Example 3:
Input: nums = [], target = 0
Output: [-1,-1]
Reasoning
Why: The runtime requirement O(log n) is the strongest clue in this prompt.
Decision: Use binary search strategy, not full scan.
Avoid: O(n) linear traversal for first/last position.

Beginner: Given Problem

Understand the prompt and why [start,end] is needed.

Detect Pattern Signals

Sorted array + O(log n) + boundary search.

Find First Position

Binary search for the left boundary.

Find Last Position

Binary search for the right boundary.

Combine Both Searches

Return [left, right], or [-1,-1] if missing.

Complexity & Code

Verify O(log n) and implementation shape.

Interactive Check

Quiz yourself on approach and expected output.

Range Finder Mastered

You can now detect boundary-search problems and solve them with two binary searches in O(log n).

Back to Problem Solving
AlgoAnimator: Interactive Data Structures