Sorting Coding Problems: 156 Questions with Solutions

156 sorting coding problems — 53 easy · 83 medium · 20 hard — with solutions in 13 languages. Plus a step-by-step walkthrough and a 8-day plan.

  • Problems: 156
  • By difficulty: 53 easy · 83 medium · 20 hard
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
  • Cost: Free on every plan; sign in to run and submit

Sorting is often the first step that makes a problem tractable — once the input is in order, duplicates sit together, the closest pair sits adjacent, intervals can be merged in one pass, and a two-pointer walk replaces a nested loop. These problems practise choosing a sort key, sorting by several keys, using a custom comparator, and knowing when a counting sort or a partial sort beats the general O(n log n).

How sorting works, step by step

sorted194271183311 − 8 = 343886answer = 3
Minimum difference between two numbers, by sorting first. Example: nums = [19, 4, 27, 11, 8, 33]
  1. The smallest difference between two of these 6 numbers could hide in any of the 15 pairs, and checking every pair is O(n²). Sorting first changes that.
  2. Sorted, in O(n log n). Now each number's closest partner is right next to it: anything further along is past that neighbour, so it is at least as far away. Only 5 neighbouring pairs are left to check.
  3. 4 and 8 are the first neighbours: 8 − 4 = 4, the best so far. The gap is written under the space between them.
  4. 8 and 11 differ by 3, smaller than 4, so the best becomes 3.
  5. 11 and 19 differ by 8, not below the best 3, which stays. Any pair further apart spans two or more of these gaps, so the smallest gap is the answer.
  6. 19 and 27 differ by 8, not below the best 3, which stays.
  7. 27 and 33 differ by 6, not below the best 3, which stays.
  8. The smallest difference is 3, between 8 and 11. Sorting cost O(n log n) and the scan 5 comparisons, so O(n log n) in all, against 15 checks unsorted.

Sorting study plan

14 of the 156 Sorting problems (4 easy, 7 medium and 3 hard) over 8 days, about 8 h 10 min in all — the pattern first, then easiest to hardest. After that, the other 142 in the full list below are practice at your own pace. Then move on to Counting Sort.

Day 1

Learn the pattern: read the essentials and step through the walkthrough above, then solve these 3.

Day 2

Medium problems: the same pattern with one twist each. Name the twist before you code.

Day 3

More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

Day 4

More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

Day 5

More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

Day 6

Hard problems: the pattern combined with a second idea. Give each a full attempt before reading the editorial.

Day 7

Another hard one. If it beats you after a real attempt, read the editorial, then solve it again tomorrow from memory.

Day 8

Another hard one. If it beats you after a real attempt, read the editorial, then solve it again tomorrow from memory.

Next topic: Counting Sort

Sorting: the essentials

When to reach for it

The answer would not change if the input were shuffled — the question is about a collection of values, not their positions — and the brute force compares every pair. "Closest", "minimum difference", "assign each … to a …", "merge the overlapping" and "largest arrangement" all point at sorting first. If the output needs original positions, sort indices or (value, index) pairs instead of the values.

The pattern

Sort once, then scan. A custom comparator must define a consistent order: negative, zero or positive, never a bare boolean, with ties broken explicitly when the output must be deterministic. When a key is costly to compute, compute it once per element rather than inside every comparison.

from functools import cmp_to_key

def largest_number(nums):           # a before b when a+b > b+a
    s = [str(n) for n in nums]
    s.sort(key=cmp_to_key(lambda a, b: (b + a > a + b) - (b + a < a + b)))
    out = "".join(s)
    return "0" if out[0] == "0" else out

Cost

Comparison sorts take O(n log n), and no comparison sort can do better in the worst case. Library sorts in Python, Java (for objects) and JavaScript are stable; C++'s std::sort is not, so use std::stable_sort when equal keys must keep their order.

Common mistakes

  • A comparator written as a - b overflows on large int values in Java or C++; use Integer.compare(a, b).
  • JavaScript's default sort() compares as strings, so [10, 9, 1] sorts to [1, 10, 9]; pass (a, b) => a - b.
  • An inconsistent comparator such as JavaScript's (a, b) => a > b, which never returns a negative and gives an engine-dependent order.
  • Sorting the input in place when a later step needs the original order.

Start with

All sorting problems

Easy (53)

Medium (83)

Hard (20)

Companies that ask sorting problems

Next topic: Counting Sort