The Find First and Last Position of Element in Sorted Array interview question asks you to locate the start and end indices of a target value within a sorted list of integers. Because the array is sorted, duplicate values will always appear contiguously. If the target is not found, you should return [-1, -1]. The key constraint is that the solution must run in logarithmic time—.
This Find First and Last Position of Element in Sorted Array coding problem is a staple at top-tier companies like Google, Apple, and Microsoft. It evaluates whether a candidate can adapt the standard Binary Search interview pattern to handle duplicate elements. It tests your ability to think critically about boundary conditions—specifically, how to modify the search to look for the "leftmost" or "rightmost" instance of a value rather than just any match.
The problem uses Binary Search. Instead of one search, you perform two variations:
Array: [5, 7, 7, 8, 8, 10], Target: 8.
[5, 7, 7, 8].[8, 10].low and high pointers, especially when deciding whether to include the mid index in the next search range.Master the "Binary Search Template." Learn how to consistently handle the while(low <= high) vs while(low < high) conditions. Being able to explain why you choose one over the other for boundary-finding problems is a sign of a strong coder.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Koko Eating Bananas | Medium | Solve | |
| Find Minimum in Rotated Sorted Array | Medium | Solve | |
| Find Peak Element | Medium | Solve | |
| Single Element in a Sorted Array | Medium | Solve | |
| Search in Rotated Sorted Array II | Medium | Solve |