Find the first and last position of a given target value within a sorted array. This problem tests your ability to efficiently search sorted data, a core skill for any software engineer.
Given a sorted array of integers in ascending order, locate the starting and ending indices of a specified target value. If the target value is absent from the array, return an array containing [-1, -1].
Imagine you're searching for a specific book in a library shelf but you don't know if it's there. You'd start from the beginning, check each book one by one until you find the first instance, and keep going until you find the last. This is a linear search, checking each element. The time complexity is O(n) because, in the worst case, you might have to scan the entire array, and the space complexity is O(1) because you're only using a constant amount of extra space.
Work through this problem with AI coaching and get real-time feedback
Practice This Problem