Reported September 2022
Bloombergsorting

Move Zeroes with Sorted Nonzero Values

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

The Bloomberg OA reported in September 2022 looks like a freebie, and that's exactly where people slip. Move Zeroes with Sorted Nonzero Values asks you to return the nonzero numbers in ascending order, then pad with the same count of zeroes. It's a filter, a sort, and a pad. The trap is in the edge cases: an empty array, an all-zero array, and negative numbers that must sort ahead of positives. If you rush it, you'll fail a hidden test you never saw. StealthCoder runs invisibly as a safety net on the live OA if you blank on the details.

The problem

Return an array containing all nonzero values from nums in ascending order, followed by the same number of zeroes as the input.

Function
moveZeroesSorted(nums: int[]) → int[]

Examples
Example 1
nums = [0,3,0,1,2]
return = [1,2,3,0,0]
The nonzero values sort to 1,2,3 and two zeroes follow.
Example 2
nums = [0,0]
return = [0,0]
There are no nonzero values.

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

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is to stop thinking about in-place zero shifting. That's the classic Move Zeroes mindset, and it doesn't help here because the nonzero values must also be sorted. Filter out the zeroes, sort what's left, then append zeroes until the length matches the input. That's O(n log n) time and O(n) space, fine for 10^5 elements. Count zeroes as you scan, or just compute the original length minus the filtered length. The pitfalls are real. Negative values are nonzero, so they belong in the sorted part and come first. An empty input should return an empty array. An all-zero input should come back unchanged. Don't sort the whole array and then rotate zeroes, because negatives will land before the zeroes and break your logic. If you freeze during the live OA, StealthCoder is the hedge that gets you the clean filter-sort-pad solution fast.

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 Move Zeroes with Sorted Nonzero Values 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

⏵ 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.

Move Zeroes with Sorted Nonzero Values FAQ

How hard is the Bloomberg Move Zeroes with Sorted Nonzero Values question really?+

It's easy on algorithm difficulty. Filter, sort, pad. The risk is carelessness, not complexity. Candidates lose points on negatives, empty arrays, and all-zero inputs. Treat it as a speed-and-correctness test and run through your edge cases before you submit.

What's the trick to solving it?+

Don't try to do it in place. Collect the nonzero values, sort them ascending, then append zeroes until the result matches the input length. The number of zeroes is the input length minus the nonzero count. It's simple, and it handles every case.

Do negative numbers count as nonzero here?+

Yes. Only the value 0 gets pushed to the end. Negatives stay in the sorted section and come first in ascending order. A solution that sorts everything and then moves zeroes can misplace them, so filter first and sort second.

What edge cases should I test before submitting?+

Test an empty array, which should return an empty array. Test all zeroes like [0,0], which should return the same thing. Test a mix with negatives, and an array with no zeroes at all. Also check duplicates, since they should stay in the sorted output.

How should I prepare in 48 hours for a Bloomberg OA like this?+

Practice array filtering, sorting with a comparator, and counting. Write a few easy array problems fast and check edge cases every time. Know your language's sort behavior, especially on integers, since some languages sort numbers as strings by default.

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