Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Sorting Algorithm Implementation

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

Your question is Sorting Algorithm Implementation. 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

A MATLAB data-processing utility needs a predictable, stable ordering for numeric signal samples before further analysis. Implement stable_merge_sort in Python to return the values in nondecreasing order without calling sorted, .sort(), or another library sorting routine.

The algorithm must be stable: if two values compare equal, their relative order must be preserved. Since the input contains primitive numeric values, stability is observed through consistent left-before-right merging when equal values are encountered.

Formal Specification

  • Input: nums, a Python list of integers or floating-point values.
  • Output: A new Python list containing the same values as nums, sorted in nondecreasing order.
  • The input list must not be modified.
  • Return an empty list when nums is empty.

Constraints

  • 0 <= len(nums) <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • Values may be integers or finite floating-point numbers
  • The input list must not be modified
  • Built-in sorting functions are not allowed

Function Signature

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