Find Peak Element
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 is the whole point of this Bloomberg OA question, reported in November 2020. A linear scan passes the examples and still fails the rule. The task is Find Peak Element: return the index of any element strictly greater than its neighbors, with the edges treated as negative infinity. The report also mentions a second question about a contiguous subarray summing to a target. If you're taking this OA in the next couple of days, the pattern is binary search on a slope, not a sorted array. StealthCoder is the safety net running invisibly on the live OA if your mind goes blank on the loop condition.
The problem
Based on the source, the other question being asked was - Given an array of positive integers and target, return True if sum of continues array equal to target. A peak element is an element that is strictly greater than its neighbors. Given a 0-indexed integer array nums, find a peak element, and return its index. If the array contains multiple peaks, return the index to any of the peaks. You may imagine that nums[-1] = nums[n] = -∞. In other words, an element is always considered to be strictly greater than a neighbor that is outside the array. You must write an algorithm that runs in O(log n) time. Function findPeakElement(nums: int[]) → int Examples Example 1 nums = [1,2,3,1] return = 2 3 is a peak element and your function should return the index number 2. Example 2 nums = [1,2,1,3,5,6,4] return = 5 Your function can return either index number 1 where the peak element is 2, or index number 5 where the peak element is 6. Constraints 1 <= nums.length <= 1000 -2^31 <= nums[i] <= 2^31 - 1 nums[i] != nums[i + 1] for all valid i.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: you don't need sorted data, you need a direction. Compare nums[mid] with nums[mid+1]. If mid is smaller, a peak must exist on the right, because the array eventually drops to negative infinity at the end. So set lo = mid + 1. Otherwise a peak exists at mid or to its left, so set hi = mid. Loop while lo < hi and return lo. The common pitfall is using lo <= hi with hi = mid, which loops forever, or reading nums[mid+1] out of bounds. Adjacent values are never equal, so there's no tie case to handle. For the subarray-sum-to-target question with positive integers, use a sliding window, since growing and shrinking the window moves the sum predictably. If you freeze on the boundary updates during the live OA, StealthCoder is the hedge that gives you the working loop.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Find Peak Element 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as find peak element. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Find Peak Element FAQ
What's the trick to Find Peak Element?+
Binary search on the slope. Compare nums[mid] to nums[mid+1]. If the right neighbor is bigger, a peak lies right of mid. If not, a peak lies at mid or left. The negative infinity edges guarantee a peak exists in whichever half you keep.
Why can't I just scan the array once?+
A scan is O(n) and the problem demands O(log n). With n up to 1000 it would pass on speed, but the stated requirement says log n, so a linear answer risks being marked wrong by the grader or reviewer. Write the binary search.
Does the array need to be sorted for binary search here?+
No. Binary search works because of the slope guarantee, not sorted order. Adjacent elements are never equal and the ends count as negative infinity, so moving toward the higher neighbor always leads to a peak.
How should I handle the subarray sum to target question?+
All values are positive, so use a sliding window. Add the right element to the sum, and while the sum exceeds the target, subtract from the left. If the sum equals the target at any point, return True. It runs in O(n) with O(1) space.
How do I prepare for this in 48 hours?+
Write the lo < hi, hi = mid version from memory three times. Test it on a single element, a two-element array, a strictly increasing array and a strictly decreasing one. Then do one sliding window problem with positive integers. That covers both reported questions.