Reported March 2020
Bloombergbinary search

Last Position of a Target in a Sorted Array

Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Bloomberg OA. Under 2s to a working solution.
Founder's read

Sorted array, a target, and you need the index of the final occurrence, or -1 if it's missing. That's the Bloomberg question reported in March 2020, and it looks like a freebie until you notice the array can hold up to 10^5 elements and duplicates. A linear scan passes the example and then dies on scale. This is binary search with a twist on what you do when you find a match. If you blank mid-assessment, StealthCoder runs invisibly on your desktop as a safety net and hands you the clean version. Know the trick first, though.

The problem

nums is sorted in nondecreasing order. Return the zero-based index of the final occurrence of target, or -1 if target is absent.

Function
lastPosition(nums: int[], target: int) → int

Examples
Example 1
nums = [1,2,2,2,3]
target = 2
return = 3
The final 2 occurs at index 3.

Constraints
0 <= nums.length <= 10^5.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is a binary search that doesn't stop on a hit. When nums[mid] equals target, record mid as the answer, then move left to mid + 1 to look for a later one. When nums[mid] is less than target go right, and when it's greater go left. Return the recorded index, or -1 if you never hit. The common pitfall is returning on the first match, which gives you some occurrence, not the last. Another is an off-by-one on an empty array, since nums.length can be 0, so make sure your loop handles lo > hi right away. A third is computing mid with overflow-prone math in languages that care. Use lo + (hi - lo) / 2. Complexity is O(log n) time and O(1) space. If you freeze on the boundary logic during the live OA, StealthCoder is the hedge that gives you the correct loop.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Last Position of a Target in a 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. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Bloomberg's OA.

Bloomberg reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Last Position of a Target in a Sorted Array FAQ

What's the trick to this Bloomberg problem?+

Don't return when you find the target. Save the index, then keep searching the right half for a later match. When the loop ends, the saved index is the last occurrence, or -1 if nothing matched. It's standard binary search with one changed branch.

How hard is it really?+

Easy on paper, but boundary bugs sink people. The logic is five lines, yet moving the wrong pointer after a match gives you the first occurrence or an infinite loop. Trace your code on [1,2,2,2,3] with target 2 before submitting.

Can I just scan from the right end?+

It's correct but O(n). With up to 10^5 elements it may pass small tests and fail larger ones. Binary search at O(log n) is what the problem is clearly asking for, since the array is sorted.

What edge cases should I test?+

Test an empty array, a single element that matches, a single element that doesn't, a target smaller than everything, a target larger than everything, and an array that's all the target. The all-duplicates case should return the final index, length minus one.

How do I prepare in 48 hours?+

Write the last-occurrence and first-occurrence variants from memory until the pointer moves feel automatic. Then do two or three related sorted-array searches. The pattern is the same each time, only the move after a match changes.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Bloomberg.

OA at Bloomberg?
Invisible during screen share
Get it