Sort a NumPy array ascending without mutating the input

Quick Overview

Return a sorted copy of a 1-D NumPy array in ascending order while leaving the input unchanged. It tests the difference between copying and in-place sorting in NumPy, how views can share memory with the input, and how descending order and NaN values are handled.

Sort a NumPy array ascending without mutating the input

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `sort_ascending(a)`: given a 1-D `np.ndarray`, return a new array with its elements in ascending order. Do not mutate the input. Use `np.sort`, which returns a copy, instead of the in-place `.sort()` method. ```python a = np.array([3, 1, 2]) out = sort_ascending(a) # out -> array([1, 2, 3]) # a -> array([3, 1, 2]) (unchanged) ``` ```hint Who owns the memory For each sorting call you know, check whether it returns a new array, reorders the existing one, or hands back something that shares memory with the input. ``` ### What a Strong Answer Covers - Returning a sorted copy and confirming the caller's array is unchanged afterward - Why `a.sort()` changes the caller's data and what it returns - How a NumPy slice or alias can look like a copy while sharing memory with the input - Time and space cost of the sort - Where NaN values end up in the result ### Follow-up Questions - How would you sort in descending order instead? - Where do NaN values land in the ascending result, and where do they land in your descending version? - If you copy with `b = a[:]` and then call `b.sort()`, is the caller's array still intact? - How would you get the indices that sort the array, with equal values kept in their original order?

Overview: Return a sorted copy of a 1-D NumPy array in ascending order while leaving the input unchanged. It tests the difference between copying and in-place sorting in NumPy, how views can share memory with the input, and how descending order and NaN values are handled.

General logo
General
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement sort_ascending(a): given a 1-D np.ndarray, return a new array with its elements in ascending order. Do not mutate the input. Use np.sort, which returns a copy, instead of the in-place .sort() method.

a = np.array([3, 1, 2])
out = sort_ascending(a)
# out -> array([1, 2, 3])
# a   -> array([3, 1, 2])  (unchanged)

What a Strong Answer Covers Guidance

  • Returning a sorted copy and confirming the caller's array is unchanged afterward
  • Why a.sort() changes the caller's data and what it returns
  • How a NumPy slice or alias can look like a copy while sharing memory with the input
  • Time and space cost of the sort
  • Where NaN values end up in the result

Follow-up Questions Guidance

  • How would you sort in descending order instead?
  • Where do NaN values land in the ascending result, and where do they land in your descending version?
  • If you copy with b = a[:] and then call b.sort() , is the caller's array still intact?
  • How would you get the indices that sort the array, with equal values kept in their original order?