Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Sort Without Native Functions

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Sort Without Native Functions. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Wpromote's Polaris analytics workflows may need deterministic ordering for metric values before generating reports. Given an array of integers, return the values in nondecreasing order without calling Python's sort() or sorted() functions.

Formal Specification

Implement sort_numbers(nums), where nums is a list of integers. Return a new list containing the same values in nondecreasing order. The input may contain duplicates, negative values, or zero. Do not rely on any native sorting function.

A merge sort implementation is expected. It should divide the input into smaller subarrays, recursively sort them, and merge the sorted results. The implementation must preserve duplicate values and handle an empty input.

Constraints

  • 0 <= len(nums) <= 100,000
  • -10^9 <= nums[i] <= 10^9
  • Do not call sort(), sorted(), or an equivalent library sorting function
  • Return a list and leave nums unchanged

Function Signature

def sort_numbers(nums):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output