First and Last Positions in a Sorted List

Easy

Question

You're given a sorted list and a target value. Return a list with the start and end positions of the target.

If the target isn't in the list, return [-1, -1].

Note: Solve the problem in O(logn) time.

Input: nums = [0, 2, 4, 5, 5, 5], target = 5

Output: [3, 5]

The first 5 is at index 3. The last 5 is at index 5.

Input: nums = [0, 2, 4, 5, 5, 5], target = 2

Output: [1, 1]

The first 2 is at index 1. The last 2 is also at index 1.

Input: nums = [1], target = 3

Output: [-1, -1]

The target 3 is not in this list.

Clarify the problem

What are some questions you'd ask an interviewer?

Understand the problem

What are the first and last positions for these inputs? nums = [1, 2, 3, 3, 3, 4, 5, 5], target = 2
[-1, -1]
[1, -1]
[-1, 1]
[1, 1]

Remember to understand the problem and plan your approach before you start coding.