Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Get started
Sort an Array Without Built-ins
00:00
5 left

Sort an Array Without Built-ins

MediumPython

Problem

How would you sort an array without using an inbuilt function?

Implement sort_array(nums) to return the elements in ascending order without calling the built-in sorting functions or other built-in sorting utilities. The input is a list of integers, and the function should return a new sorted list without modifying the original.

Input: nums, a list of integers. Output: a list containing the same values in ascending order.

Examples:

  • [5, 2, 8, 1] returns [1, 2, 5, 8].
  • [3, 3, -1, 0] returns [-1, 0, 3, 3].

Constraints: 0 <= len(nums) <= 5,000; values are between -10^9 and 10^9.

Constraints

  • 0 <= len(nums) <= 5,000
  • -10^9 <= nums[i] <= 10^9
  • Do not call sorted(), list.sort(), or another built-in sorting utility
  • Return a new list and do not modify nums

Function Signature

def sort_array(nums):
Interviewer

Your question is Sort an Array Without Built-ins. Start with the requirements in the Question tab.

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.
Code
Sign up free to run your codeLog inLn 2
Run your code to see test output here.