Sort an integer array in O(n log n) time without built-in sort functions

Quick Overview

Sort an integer array into ascending order without any built-in sort, in O(n log n) time and with as little extra space as possible. It tests whether you can implement a worst-case O(n log n) comparison sort by hand and reason about the time and space costs of different sorting algorithms.

Sort an integer array in O(n log n) time without built-in sort functions

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given an array of integers `nums`, sort the array in ascending order and return it. You must solve the problem without using any built-in sorting functions, in `O(n log n)` time and with the smallest space complexity possible. ### Function Signature ```python def sort_array(nums: list[int]) -> list[int]: ``` ### Rules - Return every element of `nums` in non-decreasing order. A value that appears several times in the input appears the same number of times in the output. - You may sort `nums` in place and return the same list. - Do not call a library sort or heap routine (`sorted`, `list.sort`, `heapq` or similar). ### Constraints - `1 <= len(nums) <= 5 * 10^4` - `-5 * 10^4 <= nums[i] <= 5 * 10^4` ### Examples ```text Input: nums = [5,2,3,1] Output: [1,2,3,5] ``` The four distinct values come back in ascending order. ```text Input: nums = [5,1,1,2,0,0] Output: [0,0,1,1,2,5] ``` Both copies of 0 and both copies of 1 are kept, so the output holds the same six elements as the input. ```text Input: nums = [3,-2,0,-2] Output: [-2,-2,0,3] ``` Negative values sort below zero, and the repeated -2 appears twice.

Overview: Sort an integer array into ascending order without any built-in sort, in O(n log n) time and with as little extra space as possible. It tests whether you can implement a worst-case O(n log n) comparison sort by hand and reason about the time and space costs of different sorting algorithms.

Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given an array of integers nums, sort the array in ascending order and return it.

You must solve the problem without using any built-in sorting functions, in O(n log n) time and with the smallest space complexity possible.

Function Signature

def sort_array(nums: list[int]) -> list[int]:

Rules

  • Return every element of nums in non-decreasing order. A value that appears several times in the input appears the same number of times in the output.
  • You may sort nums in place and return the same list.
  • Do not call a library sort or heap routine ( sorted , list.sort , heapq or similar).

Constraints

  • 1 <= len(nums) <= 5 * 10^4
  • -5 * 10^4 <= nums[i] <= 5 * 10^4

Examples

Input: nums = [5,2,3,1]
Output: [1,2,3,5]

The four distinct values come back in ascending order.

Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]

Both copies of 0 and both copies of 1 are kept, so the output holds the same six elements as the input.

Input: nums = [3,-2,0,-2]
Output: [-2,-2,0,3]

Negative values sort below zero, and the repeated -2 appears twice.