First and Last Positions in a Sorted List
Easy
Question
Given a sorted list, return a list with the start and end positions of a given target value.
If the target isn't in the list, then 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 found at index 3 in the list, and the last 5 is found at index 5 in the list.
Input: nums = [0, 2, 4, 5, 5, 5], target = 2
Output: [1, 1]
The first 2 is found at index 1 in the list, and the last 2 is also at index 1 in the list the list.
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 is the first and last positions if given these inputs? nums = [1, 2, 3, 3, 3, 4, 5, 5], target = 2
[-1, -1]
[1, -1]
[-1, 1]
[1, 1]
Take a moment to understand the problem and think of your approach before you start coding.