Reported April 2022
ZipRecruitersorting

Sort an Integer Array in Ascending Order

Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live ZipRecruiter OA. Under 2s to a working solution.
Founder's read

The ZipRecruiter OA reported in April 2022 looks like a trick, but it's just sorting. Return the array in nondecreasing order, with duplicates and negatives allowed. The real question is whether you can write an O(n log n) sort without leaning on a shortcut the grader might frown on. With up to 100000 elements, bubble sort and insertion sort will die. If you've got an invite in your inbox, expect this to be a check on fundamentals, not cleverness. StealthCoder is there as a safety net on the live OA if you blank on merge sort or heap sort mid-assessment, but you probably won't need it.

The problem

Return the values of values in nondecreasing order. Duplicate and negative values are allowed.

Function
sortAscending(values: int[]) → int[]

Examples
Example 1
values = [1,2,4,3]
return = [1,2,3,4]
The out-of-order final pair is corrected.
Example 2
values = [3,1,3,2]
return = [1,2,3,3]
Equal values are retained.

Constraints
0 <= values.length <= 100000
-1000000000 <= values[i] <= 1000000000

Reported by candidates. Source: FastPrep

Pattern and pitfall

What this really reduces to: pick a sort that runs in O(n log n) worst case and doesn't recurse into trouble. Merge sort is the safe pick. Split, sort halves, merge with a temp array. Heap sort works too and sorts in place. The common pitfall is naive quicksort with a bad pivot, which degrades to O(n^2) on already sorted input, and recursion depth can blow the stack at 100000 elements. Random pivot or median of three fixes it. Second pitfall: integer overflow if you compute differences as comparators with values near 1e9 and -1e9. Compare with less-than, never subtract. Handle the empty array first. Duplicates need no special treatment, but keep the merge using <= so it stays stable. If the assessment allows built-ins, one call finishes it, but if it bans them and your merge logic slips, StealthCoder is the hedge during the live OA.

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 Sort an Integer Array in Ascending Order 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

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as sort an array. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass ZipRecruiter's OA.

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

Sort an Integer Array in Ascending Order FAQ

How hard is the ZipRecruiter sort an integer array problem really?+

Easy if built-ins are allowed, medium-easy if you must implement the sort yourself. The only real difficulty is writing merge sort or heap sort cleanly under pressure. There's no hidden twist in the problem. Duplicates and negatives need no special handling.

What's the trick to passing all the test cases?+

Use a guaranteed O(n log n) algorithm. With 100000 elements, anything O(n^2) will time out. Merge sort is the most reliable choice. Also handle the empty array, since length 0 is allowed, and compare values directly instead of subtracting them.

Can I just call the language's built-in sort?+

The problem text doesn't forbid it, and a built-in sort handles every constraint here. But if the assessment is meant to test fundamentals, it may restrict it. Read the instructions on screen first. Have a merge sort ready in case the built-in is disallowed.

Why does quicksort sometimes fail on this one?+

A fixed pivot like the first or last element degrades to O(n^2) on sorted or reverse-sorted input. At 100000 elements that means timeouts or deep recursion errors. If you use quicksort, pick a random pivot or median of three, or just switch to merge sort.

How do I prepare for this in 48 hours?+

Write merge sort from memory twice, once recursive and once with a helper merge function. Test on an empty array, one element, all duplicates, and mixed negatives. Then do a quick heap sort as a backup. That covers this problem and most of its variants.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with ZipRecruiter.

OA at ZipRecruiter?
Invisible during screen share
Get it