# Sort Descending

> Biggest first. No exceptions.

Canonical URL: <https://datadriven.io/problems/sort_descending>

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

Given a list of numbers, return a new list sorted in descending order. Do not modify the input.

## Worked solution and explanation

### Why this problem exists in real interviews

Sort a list without mutating the input is a one-liner that tests two specific things: do you know sorted() returns a new list and list.sort() mutates in place, and do you remember the reverse=True flag. Interviewers slip this in to check Python fluency before harder work. The single most common failure is calling nums.sort(reverse=True) and returning None implicitly.

---

### Break down the requirements

#### Step 1: Use sorted() with reverse=True

sorted(nums, reverse=True) returns a new list in descending order. sorted() is the free function; list.sort() is the in-place method. The spec says 'do not modify the input,' which is the exact distinction between these two APIs.

#### Step 2: Return the sorted list directly

sorted() returns a list, so return its value. No need for a temporary variable, no need for .copy() (sorted already produces a new object). One expression, one return.

#### Step 3: Handle empty and single-element lists without special cases

sorted([]) == [] and sorted([1], reverse=True) == [1]. The Timsort implementation handles these in constant time without a branch in your code. Writing 'if not nums: return []' is a tell that you do not trust the standard library.

---

### The solution

**sorted() with reverse=True**

```python
def sort_descend(nums):
    return sorted(nums, reverse=True)
```

> **Cost Analysis**
>
> Python uses Timsort: O(n log n) worst case, O(n) on already-sorted or reverse-sorted input. Space is O(n) for the new list (sorted copies before sorting). Timsort is implemented in C and is among the fastest comparison sorts in any language; do not try to beat it with a hand-rolled algorithm.

> **Interviewers Watch For**
>
> Whether you know sorted() vs list.sort() without hesitating, whether you reach for reverse=True instead of sorted(nums)[::-1] (which also works but allocates twice), and whether you remember the input-immutability constraint. Strong candidates mention key= for custom orderings and the stability guarantee (equal elements keep their relative order).

> **Common Pitfall**
>
> Calling nums.sort(reverse=True) and returning nums. This mutates the input, which violates the spec, AND the return value of .sort() is None, so if you wrote 'return nums.sort(reverse=True)' the function returns None. Another miss: sorted(nums, key=lambda x: -x), which fails on non-numeric types and costs an extra function call per element.

---

## Common follow-up questions

- How would you sort a list of tuples by the second element descending, with ties broken by the first element ascending? _(sorted(pairs, key=lambda p: (-p[1], p[0])); or two sorts leveraging stability (sort by primary first, then by secondary, reversed order).)_
- What changes if the list is too large to fit in memory? _(heapq.nlargest if you only need the top k; otherwise external merge sort or a database with ORDER BY. Mention that Timsort is in-memory only.)_
- How would you sort a list of dicts by a key that may be missing? _(key=lambda d: d.get('field', default); discuss sentinel values for missing keys (e.g., float('inf') for descending sort of missing-last).)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/sort_descending)
- [Python Interview Questions](https://datadriven.io/python-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.