Monotonic Array
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Bloomberg reported this one in October 2020, and the hinted pattern says stack, but you don't need one. The solution hinges on a single pass and two boolean flags, no data structure at all. If you're sitting on an OA invite, that's the good news: Monotonic Array is short, and the only way to lose it is overthinking. Check whether the array ever goes up and whether it ever goes down. If both happen, it's not monotone. If you blank on the edge cases mid-assessment, StealthCoder runs invisibly on your desktop as a safety net while you work.
The problem
Return whether nums is monotone. It is monotone when it is entirely nondecreasing or entirely nonincreasing. Function isMonotonic(nums: int[]) → boolean Examples Example 1 nums = [1,2,2,3] return = true The array is nondecreasing. Example 2 nums = [1,3,2] return = false The direction changes. Constraints 1 <= nums.length <= 10^5. Values are 32-bit signed integers.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that you don't pick a direction up front. Walk the array once, compare each neighbor pair, and track two flags: increasing and decreasing. If nums[i] > nums[i-1], set increasing. If nums[i] < nums[i-1], set decreasing. Return false the moment both are true. Equal neighbors set neither flag, which is why [1,2,2,3] passes. The common pitfall is using strict comparisons for the whole array, or deciding direction from the first two elements, which breaks when they're equal. A length of 1 should return true, and an all-equal array is both nondecreasing and nonincreasing. With n up to 10^5 this is O(n) time and O(1) space, so no sorting and no comparing against a sorted copy. The stack hint is a red herring here. If the live OA throws a twist and your head goes quiet, StealthCoder is the hedge that reads the screen and hands you the clean version.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Monotonic 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 StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as monotonic array. 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. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Monotonic Array FAQ
How hard is Monotonic Array really?+
Easy. It's one loop and two flags. The difficulty is in the edge cases: equal neighbors, a single element, and arrays that are all the same value. If you handle those, you're done in a few minutes.
Do I actually need a stack for this Bloomberg problem?+
No. The stack hint doesn't match the simplest solution. A single pass with increasing and decreasing flags runs in O(n) time and O(1) space. A stack adds memory and code for no benefit here.
What's the trick to getting it right the first time?+
Don't decide direction early. Track whether you've ever seen a rise and whether you've ever seen a fall. If both flags are true, return false. Equal neighbors change nothing, so they never break monotonicity.
What edge cases should I test before submitting?+
Test a length-1 array, an all-equal array like [5,5,5], a strictly increasing one, a strictly decreasing one, and [1,3,2]. Also try a case with leading equal values, like [2,2,1], to catch direction-guessing bugs.
How do I prepare for this in 48 hours?+
Write it from scratch twice, once with two flags and once with a single direction variable. Then run your edge cases by hand. Since Bloomberg reported it in October 2020, expect array-scan problems like this one to show up again in similar forms.