Find First and Last Position in Sorted Array
Reported by candidates from Tennr's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Tennr OA reported in February 2025 hands you a sorted array and a target, and the naive version fails the moment duplicates show up. Find the first and last index of the target in O(log n). One binary search finds a match, but not the boundaries. If you've got the OA in a day or two, this is the one to nail cold. It's a binary search with a twist, and the twist is the whole question. StealthCoder runs invisibly as a safety net if your mind goes blank mid-assessment, but the pattern below should get you most of the way.
The problem
You are given an integer array nums sorted in non-decreasing order and an integer target. Return [first, last], the first and last index where target occurs. If it is absent, return [-1, -1]. Your algorithm must run in O(log n) time. Function searchRange(nums: int[], target: int) → int[] Examples Example 1 nums = [5,7,7,8,8,10] target = 8 return = [3,4] The target occupies indices 3 and 4. Example 2 nums = [5,7,7,8,8,10] target = 6 return = [-1,-1] The target is absent. Constraints 0 <= nums.length <= 100000. -10^9 <= nums[i], target <= 10^9. nums is sorted in non-decreasing order.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is running binary search twice, once for the leftmost index and once for the rightmost. Keep a result variable. When you hit the target, record the index and keep searching: move right = mid - 1 for the first position, left = mid + 1 for the last. Don't stop at the first match. The naive approach finds one hit, then walks outward linearly. That's O(n) on an array full of the same value, and it breaks the O(log n) requirement. Watch the edge cases: empty array (length 0 is allowed), target smaller than everything, target larger than everything, and a single element. Use mid = left + (right - left) / 2 to avoid overflow habits. Return [-1, -1] if the first search finds nothing. If you freeze on the boundary logic during the live OA, StealthCoder is the hedge that gives you the working template. Write both searches as one helper with a boolean flag.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Find First and Last Position in Sorted Array cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as find first and last position of element in sorted array. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Tennr's OA.
Tennr reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Find First and Last Position in Sorted Array FAQ
What's the trick in Find First and Last Position in Sorted Array?+
Don't stop at the first match. Run binary search twice. When you find the target, save the index and keep shrinking toward the left side for the first position, then toward the right side for the last. Two O(log n) passes give you both boundaries.
How hard is this problem really?+
It's medium, but it's mostly a binary search you already know with one changed branch. The difficulty is off-by-one errors and the continue-after-match logic. If you can write a standard binary search from memory, you can solve this in about fifteen minutes.
Why does the linear expansion approach fail?+
Finding one match and walking outward is O(n) when the whole array is the target, like a hundred thousand copies of the same number. The problem demands O(log n), so the OA's hidden tests will likely punish that approach with a timeout.
What edge cases should I test for the Tennr OA?+
Test an empty array, a single element that matches, a single element that doesn't, a target below the minimum, a target above the maximum, and an array where every element equals the target. Also check a target that falls between two existing values.
How do I prepare for this in 48 hours?+
Write the leftmost and rightmost binary search from scratch three times without looking. Then solve it with a single helper that takes a flag. Practice the lower-bound idea too, since many sorted-array questions reduce to it. Focus on loop conditions and pointer updates.