Roadmap › Stage 1: Foundations (Start Here) › Pattern 4 of 53
DSA pattern guide

Sorting — Basics

Order makes the answer obvious: neighbours become comparable, duplicates sit together, greedy choices become safe.

Stage 1 · Foundations (Start Here)5 problems3 Easy · 2 Medium

Use it when

Order makes the answer obvious: neighbours become comparable, duplicates sit together, greedy choices become safe.

Key idea

Sort first (O(n log n)), then a single pass. Write merge sort once yourself in Sort an Array so you understand what the library does.

Practise it right here

3 original problems for this pattern in our free in-browser console. Write Java, Python or JavaScript and get an instant verdict, no install needed.

Open the practice console

Practice problems, in order

  1. Height CheckerEasy
  2. Relative Sort ArrayEasy
  3. Largest Perimeter TriangleEasy
  4. Sort an ArrayMedium
  5. H-IndexMedium

How to practise this pattern

  1. Read the problem and look for the signal above before writing any code.
  2. Say the brute force out loud, with its time complexity, then ask what work it repeats.
  3. Apply the key idea and write the optimised version yourself.
  4. If you're stuck for more than 10 minutes, take one small hint, not the full solution.
  5. Move to the next problem only when you can explain why this pattern fits.

Stuck on one of these?

The AlgoSolveo extension (free, launching soon) sits beside the LeetCode problem and gives the smallest hint that gets you moving, never the full answer. Solved problems tick off on your roadmap.

Join the extension waitlistTrack it on the roadmap
← Previous patternHashing — BasicsNext pattern →Matrix — 2D Arrays