Dsa
Binary Search
Hard
Find First and Last Position of an Element in Sorted Array
Given a sorted array of integers and an integer target, find the starting and ending position of the target in the array. If the target is not found, return [-1, -1]. You must write an algorithm with O(log n) runtime complexity.
Input format:
First line contains integer n (size of the array).
Second line contains n integers (the elements of the sorted array).
Third line contains the target integer.
Output format:
A list of two integers, representing the starting and ending position of the target.
Example:
Input:
7
5 7 7 8 8 10
8
Output:
[3, 4]
Key concepts
binary_searcharrayspositions
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now