AI InterviewSearch…Search problems, topics, or pages…Ctrl KGet started

Topic

Sorting coding problems

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.

New to sorting? Read the Sorting Algorithms tutorial first.

274 problems · 67 easy · 155 medium · 52 hard · judged by hidden tests in 13 languages

274problems

67 easy

155 medium

52 hard

Sign in to tick off what you solve.

Study plan: 14 problems · about 8 h 10 min

Start the study plan

Next up: Contains Duplicate

How it works

Sorting, step by step

Minimum difference between two numbers, by sorting first

nums = [19, 4, 27, 11, 8, 33]

nums1942711833pairs to check: 15

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.

0:01 / 0:23Step 1 of 8
Transcript · 8 steps

Study plan

Learn sorting in 8 days

14 of the 274 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 260 in the full list below are practice at your own pace. Then move on to Counting Sort.

  1. Day 1

    about 1 h 15 min

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

  2. Day 2

    about 55 min

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

  3. Day 3

    about 1 h 10 min

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

  4. Day 4

    about 1 h 10 min

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

  5. Day 5

    about 1 h 10 min

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

  6. Day 6

    about 50 min

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

  7. Day 7

    about 50 min

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

  8. Day 8

    about 50 min

    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 Coding Problems: 274 Questions with Solutions

274 sorting coding problems — 67 easy · 155 medium · 52 hard — with solutions in 13 languages. Plus a step-by-step walkthrough and a 8-day plan.

  • Problems: 274
  • By difficulty: 67 easy · 155 medium · 52 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).

New to sorting? Read the Sorting Algorithms tutorial first.

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 274 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 260 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 (67)

Medium (155)

Hard (52)

Companies that ask sorting problems

Next topic: Counting Sort