Search a Valley Array
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The O(log n) requirement in this Bloomberg OA, reported in November 2020, kills the linear scan before you type it. The array is a valley: strictly decreasing to one minimum, then strictly increasing. Distinct values, up to 10^5 elements, find the target's index or return -1. It's a binary search problem wearing a costume. If you've seen rotated array search, you've got the instinct. If your brain locks up mid-assessment, StealthCoder runs invisibly on your desktop as a safety net and hands you a working solution while the proctor sees nothing.
The problem
nums contains distinct values, strictly decreasing through one minimum and strictly increasing afterward. Return the index of target, or -1 if absent, in O(log n) time. Function searchValley(nums: int[], target: int) → int Examples Example 1 nums = [10,4,3,2,5,6,8] target = 3 return = 2 3 occurs on the decreasing side before the minimum 2. Constraints 1 <= nums.length <= 10^5.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is two phases. First, binary search for the minimum: compare nums[mid] with nums[mid+1]. If nums[mid] > nums[mid+1], you're still on the decreasing side, so lo = mid+1. Otherwise hi = mid. That lands on the valley index. Second, run a standard binary search on the decreasing segment [0, valley] with reversed comparisons, then on the increasing segment [valley+1, n-1] with normal ones. Return whichever hits. The common pitfall is checking the valley itself twice or flipping the comparison on the descending half, which sends you the wrong way. Edge cases: length 1, the minimum at index 0 or n-1 (a purely monotonic input), and target absent. Total cost is three O(log n) passes. If you blank on the boundary logic during the live OA, StealthCoder is the hedge that gives you the code and the reasoning without anyone seeing it.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Search a Valley 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 StealthCoderRelated leaked OAs
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 a Valley Array FAQ
What's the trick in the Bloomberg Search a Valley Array problem?+
Find the minimum first with binary search by comparing nums[mid] to nums[mid+1]. Then binary search each side separately. The left side is descending, so flip your comparison. The right side is ascending, so use the normal one. Three log passes, still O(log n).
How hard is this problem really?+
Medium. The idea is simple once you see it, but the boundary conditions trip people up. Off-by-one errors in finding the minimum and reversed comparison logic on the descending half cause most wrong answers. Expect to test small arrays by hand.
Can I do it in a single binary search pass?+
Yes, but it's messier. You compare target and nums[mid] along with the slope at mid to decide direction, with several cases. The three-pass approach is cleaner, easier to debug under pressure, and has the same asymptotic complexity, so most candidates should use it.
What edge cases should I test before submitting?+
Test length 1, a valley at index 0 or the last index (fully monotonic), a target equal to the minimum, a target smaller than everything, and a target larger than everything. Also check the example: [10,4,3,2,5,6,8] with target 3 should return 2.
How do I prepare for this in 48 hours?+
Practice binary search on a rotated sorted array and on a peak or mountain array. Write the find-minimum loop from memory using lo < hi and hi = mid. Then write a descending binary search once. That's the whole toolkit for this problem.