Sort an Array

MediumArrays & Hashingdivide and conquer

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Given an integer array nums, return a new array containing the same values in non-decreasing order. Do not call any built-in sorting function. Implement a comparison sort that runs in O(n log n) time, such as merge sort or heap sort. Duplicates and negative numbers must be preserved.

Examples

Input: nums = [4,-1,3,-1,0]
Output: [-1,-1,0,3,4]
Both copies of -1 are kept and everything is in ascending order.
Input: nums = [7]
Output: [7]
A single element is already sorted.

Constraints

  • 1 <= nums.length <= 5 * 10^4
  • -5 * 10^4 <= nums[i] <= 5 * 10^4
  • No built-in sort

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More divide and conquer problems