First and Last Position in a Sorted Array
Updated
Problem statement
The integer array nums is sorted from smallest to largest. Return the first and last indices containing target.
If the target is absent, return [-1,-1]. Your algorithm must run in O(log n) time, where n is the array length.
Function
searchRange(nums: int[], target: int) → int[]Examples
Example 1
nums = [5,7,7,8,8,10]target = 8return = [3,4]The target appears at positions 3 and 4.
Example 2
nums = [5,7,7,8,8,10]target = 6return = [-1,-1]The target does not occur.
Example 3
nums = []target = 0return = [-1,-1]An empty array contains no target.
Constraints
0 <= nums.length <= 10^5.-10^9 <= nums[i], target <= 10^9.numsis sorted in nondecreasing order.
More Apple Phone Screen questions
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Topological OrderingPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Merge IntervalsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Subarray Product Less Than KPHONE SCREEN · Seen Nov 2026
- Evaluate DivisionPHONE SCREEN · Seen Nov 2026
- Reverse a Linked ListPHONE SCREEN · Seen Nov 2026
- Validate a BST with a Custom ComparisonPHONE SCREEN · Seen Nov 2026
- Design a Food Rating SystemPHONE SCREEN · Seen Oct 2026
- Days Until Poisoned Plants StabilizePHONE SCREEN · Seen Oct 2026
- Capture Surrounded Regions with Union-FindPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Single Element in a Sorted ArrayGreyOrange · PHONE SCREENGreyOrange interview questions
- Count Winning Dice Face PairsGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Search in a Rotated Sorted ArrayAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Longest Increasing SubsequenceByteDance · PHONE SCREENByteDance interview questions
- Koko Eating BananasThe D.E. Shaw Group · PHONE SCREENThe D.E. Shaw Group interview questions
- Find First and Last Position in a Sorted ArrayInstacart · PHONE SCREENInstacart interview questions
- Longest Increasing SubsequenceAngel One · PHONE SCREEN
- Search a 2D MatrixWhatNot · PHONE SCREEN · ONSITE INTERVIEWWhatNot interview questions
- Find First and Last Position of Element in Sorted ArrayEPAM · PHONE SCREENEPAM interview questions
- Find K Closest Elements in a Sorted ArrayNavi · PHONE SCREEN