Reported November 2022
Bloombergbinary search

Search in Rotated 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

Bloomberg reportedly served this one in November 2022, and the trap isn't the idea, it's the boundary check. Search in Rotated Sorted Array looks like a textbook binary search until a single off-by-one sends you into an infinite loop or returns -1 on an array that clearly holds the target. The array is sorted, then rotated at an unknown pivot, values are distinct, and you need O(log n). If your brain freezes on the comparisons, StealthCoder is the invisible safety net running during the live OA. But you can own this in an evening.

The problem

Given an integer array nums that was sorted in strictly increasing order and then rotated at an unknown pivot, and an integer target, return the index of target.
Return -1 when target does not appear in nums.
All values in nums are distinct. Your solution must run in O(log n) time.

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

Examples
Example 1
nums = [4,5,6,7,0,1,2]
target = 0
return = 4
The target 0 appears at index 4.
Example 2
nums = [4,5,6,7,0,1,2]
target = 3
return = -1
The target 3 is absent, so the result is -1.
Example 3
nums = [1]
target = 0
return = -1
The only array value is 1, so 0 is absent.

Constraints
1 <= nums.length <= 10^5
-10^9 <= nums[i] <= 10^9
nums contains distinct values.
nums was sorted in strictly increasing order and rotated at an unknown pivot.
-10^9 <= target <= 10^9

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: at every step, at least one half of the window [lo, hi] is properly sorted. Compute mid. If nums[lo] <= nums[mid], the left half is sorted. Check if target sits in [nums[lo], nums[mid]). If yes, move hi to mid-1, otherwise move lo to mid+1. If the left isn't sorted, the right half is, so do the mirror check with nums[mid] < target <= nums[hi]. The edge case that kills naive solutions is the <= in nums[lo] <= nums[mid]. With two elements, lo equals mid, and a strict < sends you to the wrong half. Also test a single element and an array that isn't rotated at all. Don't find the pivot first and then search twice unless you want more code to get wrong. If you blank mid-assessment, StealthCoder can surface the one-pass version while you keep typing.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Search in Rotated 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as search in rotated sorted array. If you have time before the OA, drill that.

⏵ The honest play

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

Bloomberg reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Search in Rotated Sorted Array FAQ

What's the trick to Search in Rotated Sorted Array?+

One half of any window is always sorted. Find which half using nums[lo] <= nums[mid], check whether the target falls inside that sorted half, and discard the other half. That keeps it O(log n) in a single pass with no pivot search.

How hard is this Bloomberg OA question really?+

It's medium. The concept is simple, but boundary conditions trip people up. Most failures come from using < instead of <= when comparing nums[lo] and nums[mid], or from mishandling inclusive versus exclusive bounds on the target check.

Which edge cases should I test before submitting?+

Test a single-element array, a two-element array like [3,1], an array that wasn't actually rotated, the target at the first or last index, and a target that's absent. Two-element arrays catch the most bugs because lo and mid land on the same index.

Can I find the pivot first and then binary search?+

Yes, it works and stays O(log n), but it means two binary searches and more places for off-by-one errors. The single-pass approach is shorter. Use two passes only if the single-pass logic doesn't click for you.

How do I prepare for this in 48 hours?+

Write the single-pass solution from scratch three times without looking. Then hand-trace [4,5,6,7,0,1,2] with targets 0 and 3, plus [3,1] with target 1. Once you can trace the pointer moves cleanly, the pattern will hold under pressure.

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