Reported January 2022
Bloombergarray

Non-decreasing 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

With nums.length up to 10^5, trying every possible edit and rechecking the array is a quadratic dead end, and Bloomberg's January 2022 OA report on Non-decreasing Array is built to punish that instinct. The task is simple to state: can you make the array nondecreasing by changing at most one element to any integer? It's an array scan with a greedy fix, and it takes one pass. If you've got the OA invite and 48 hours, learn the single decision at the first drop. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment.

The problem

Return whether nums can become nondecreasing by modifying at most one element to any integer value.

Function
canBeNonDecreasing(nums: int[]) → boolean

Examples
Example 1
nums = [4,2,3]
return = true
Change 4 to 1.
Example 2
nums = [4,2,1]
return = false
One change cannot repair both drops.

Constraints
1 <= nums.length <= 10^5.
Values are 32-bit signed integers.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is to scan once and act at the first index i where nums[i] > nums[i+1]. You have two repair options. Lower nums[i] to nums[i+1], or raise nums[i+1] to nums[i]. Pick based on nums[i-1]. If i is 0 or nums[i-1] <= nums[i+1], lower nums[i], which keeps the prefix valid and the next value small. Otherwise raise nums[i+1] to nums[i]. Count modifications and return false on the second drop. The common pitfall is always choosing one option, which fails on cases like [3,4,2,3] or [4,2,3]. Another is checking only adjacent pairs without looking back at nums[i-1]. Because values are 32-bit integers, you never need to worry about running out of room to modify. Runtime is O(n), space O(1). If the greedy choice slips your mind live, StealthCoder is the hedge that hands you the working solution while you keep typing.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Non-decreasing 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. If you're reading this with an OA window open, you're who this was built for.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as non decreasing 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. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Non-decreasing Array FAQ

What's the trick to Non-decreasing Array?+

Find the first place where nums[i] > nums[i+1] and fix it greedily. Compare nums[i-1] with nums[i+1]. If the earlier element fits under nums[i+1], lower nums[i]. Otherwise raise nums[i+1] to nums[i]. A second drop means false.

Why doesn't brute force work here?+

With length up to 10^5, trying every index and every replacement value and re-validating is at least O(n^2), and the value range is huge. The greedy single pass is O(n) and only needs a counter of changes made.

What edge cases should I test before submitting?+

Test length 1, which is always true. Test [4,2,3] true and [4,2,1] false from the examples. Also test [3,4,2,3], which is false, and a drop at index 0 like [5,1,2], which is true. These catch the wrong-direction fix bug.

Is this Bloomberg question still worth preparing for?+

It was reported in January 2022, and the pattern is a standard array greedy scan. Variants with one allowed edit show up often, so knowing the lower-or-raise decision is cheap and transfers to similar OA questions.

How do I prepare for this in 48 hours?+

Write the one-pass solution from scratch twice, then hand-trace [3,4,2,3], [4,2,3], and [4,2,1]. Aim to explain why you lower nums[i] versus raise nums[i+1]. That's about an hour of work, and it's enough for this problem.

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