DSA Tutorial
🔍

Sorting Practice Problems

How to Use This Problem List

Sorting problems in interviews divide cleanly into two categories: problems where sorting is the entire solution, and problems where sorting is the first step that unlocks a faster technique (two-pointer, binary search, greedy scan). Recognising which category a problem belongs to is the key skill.

The second insight: many problems that look different on the surface — merge intervals, meeting rooms, task scheduling — all have "sort first, then linear scan" as their core pattern.

Practice one pattern at a time. After four or five problems from the same pattern, the approach becomes automatic.

Pattern 1: Sort and Scan — Greedy After Sorting

Sort first to establish order, then apply a single linear scan to solve the problem greedily. Sorting converts a multi-dimensional overlap/scheduling/grouping problem into a one-dimensional sequence problem.

ProblemDifficultyRecognition ClueKey Insight
Merge IntervalsMedium"merge overlapping intervals"Sort by start; scan and merge when next.start ≤ current.end
Meeting RoomsEasy"can attend all meetings without overlap"Sort by start time; check if any consecutive pair overlaps
Meeting Rooms IIMedium"minimum conference rooms needed"Sort by start; min-heap of end times — greedy room assignment
Non-overlapping IntervalsMedium"minimum removals to make non-overlapping"Sort by end time; greedy keep earliest-ending intervals
Minimum Number of ArrowsMedium"minimum arrows to burst all balloons"Sort by end; greedy one arrow per overlapping group
Car FleetMedium"cars that arrive together form a fleet"Sort by position (descending); track when faster car is caught
Task SchedulerMedium"minimum intervals to finish tasks with cooldown n"Sort by frequency; max-heap fills cooldown slots
Hand of StraightsMedium"can hand be divided into groups of k consecutive"Sort; greedily consume each group starting from smallest

Complexity target: O(n log n) for sort + O(n) scan = O(n log n) total.

Recognition signal: "intervals", "meetings", "overlapping", "scheduling", "arrange groups". The problem involves ranges or events that interact based on their order.

Pattern 2: Custom Comparator Sorting

The sort key or comparison rule is non-trivial — not simply ascending or descending by one field. The problem is solved entirely by defining the right comparison function.

ProblemDifficultyRecognition ClueKey Insight
Largest NumberMedium"arrange integers to form largest number"Custom comparator: compare ab vs ba as strings
Sort Characters by FrequencyMedium"sort by frequency descending"Build frequency map; sort by (−freq, char) or max-heap
Sort Array by ParityEasy"even numbers first, odd last"Partition in-place: two-pointer even/odd separation
Sort Array by Parity IIEasy"even indices have even elements"Two-pointer: fill even indices from even pool, odd from odd
Rank Teams by VotesMedium"rank candidates by vote positions"Custom comparator: compare vote counts at each position
Reorder Data in Log FilesMedium"letter-logs before digit-logs, letter-logs sorted"Stable sort: key by (log-type, content, identifier)
Wiggle SortMedium"nums[0] ≤ nums[1] ≥ nums[2] ≤ ..."Sort then interleave small/large halves, or one-pass swap
Sort Colors (Dutch National Flag)Medium"sort array of 0s,1s,2s in one pass"3-way partition: lt/mid/hi pointers, O(n) O(1)
Minimum Absolute DifferenceEasy"find all pairs with minimum absolute difference"Sort; scan adjacent pairs — minimum diff is between neighbours

Complexity target: O(n log n) with custom comparator.

Recognition signal: "arrange to maximise/minimise", "sort by custom rule", "group by property", "wiggle", "interleave". The difficulty is defining the comparison, not the sorting itself.

Pattern 3: Sort and Binary Search

Sort the collection once, then answer multiple queries with binary search. The amortised cost is O(n log n + q log n) instead of O(q × n).

ProblemDifficultyRecognition ClueKey Insight
Two Sum II (sorted array)Easy"sorted array, find pair summing to target"Two-pointer on sorted array — O(n) after sort
3SumMedium"find all triplets summing to zero"Sort; for each element binary search or two-pointer for pair
4SumMedium"find all quadruplets summing to target"Sort; nested loops + two-pointer for remaining pair
Find Target in Rotated ArrayMedium"binary search in rotated sorted array"Sort first or use rotated binary search
H-IndexMedium"maximum h papers with ≥ h citations"Sort citations descending; scan until citations[i] < i+1
Search a 2D MatrixMedium"row-sorted, globally sorted matrix"Treat as flat sorted array; binary search with row/col mapping
Count Pairs With Absolute Diff ≤ KEasy"count pairs (i,j) witharr[i]-arr[j]
Missing RangesEasy"find ranges of missing numbers"Sort; scan gaps between consecutive elements

Complexity target: O(n log n) sort + O(log n) per query = O(n log n + q log n).

Recognition signal: "sorted array", "find pair/triple/quadruple", "k closest", "repeated queries on the same data". Sort once, query many times.

Pattern 4: QuickSelect and Order Statistics

Finding the k-th smallest/largest element, or the median, without sorting the entire array. QuickSelect achieves O(n) average using the partition step from QuickSort.

ProblemDifficultyRecognition ClueKey Insight
Kth Largest Element in ArrayMedium"k-th largest, O(n) preferred"QuickSelect: partition, recurse into one side only
Find Median from Data StreamHard"dynamic median as elements arrive"Two heaps: max-heap (lower half) + min-heap (upper half)
Kth Smallest in Sorted MatrixMedium"k-th smallest in row/col sorted matrix"Binary search on value + count; or min-heap
Wiggle Sort IIHard"nums[0] < nums[1] > nums[2] < ..."QuickSelect to find median; 3-way partition; virtual indexing
Top K Frequent ElementsMedium"k most frequent elements"Frequency map + QuickSelect on frequencies, or min-heap
K Closest Points to OriginMedium"k points with smallest distance"QuickSelect on distance², or max-heap of size k
Minimum Cost to Hire K WorkersHard"hire k workers minimising total wage"Sort by wage/quality ratio; sliding sum with min-heap

Complexity target: O(n) average for QuickSelect, O(n log k) for heap-based.

Recognition signal: "k-th largest/smallest", "k closest", "k most frequent", "find median". The problem asks for a rank or order statistic, not a full sort.

Pattern 5: Counting and Bucket Sort Applications

Problems where the natural domain (small integer range, character frequencies) allows O(n) sorting or grouping without comparison.

ProblemDifficultyRecognition ClueKey Insight
Sort Colors (0s, 1s, 2s)Medium"three distinct values in one pass"Dutch National Flag: 3-way partition, O(n) O(1)
Maximum GapHard"maximum difference between successive elements"Bucket Sort: n+1 buckets, max gap crosses bucket boundaries
Top K Frequent WordsMedium"k most frequent strings"Frequency map + bucket sort by frequency → O(n)
Group AnagramsMedium"group strings that are anagrams"Sort each string as key → group by sorted key
Valid AnagramEasy"are two strings anagrams"Frequency count of each character; compare counts
Find All Anagrams in StringMedium"find all anagram starting positions"Sliding window + character frequency comparison
Minimum Deletions to Make Array BeautifulMedium"alternating parity after minimal deletions"Count sort by parity category
Sort Array by Increasing FrequencyEasy"sort by frequency ascending, then by value descending"Frequency map; custom sort key: (freq, -val)

Complexity target: O(n + k) where k is the range or number of distinct values.

Recognition signal: "sort with limited values", "character frequencies", "group by property", "anagram", "bucket". The domain is bounded — use counting/bucket sort instead of comparison sort.

Pattern 6: Permutations, Cycles, and Inversion Counting

Problems rooted in permutation theory — minimum swaps, inversion counts, cycle detection in rearrangement problems.

ProblemDifficultyRecognition ClueKey Insight
Count InversionsMedium"pairs (i,j) where i<j but arr[i]>arr[j]"Merge sort: count right-element picks during merge phase
Minimum Swaps to Sort ArrayMedium"minimum swaps to sort using position mapping"Build permutation graph; swaps = n − number_of_cycles
Couples Holding HandsHard"minimum swaps so each couple sits together"Permutation cycles on couple positions
Minimum Number of Swaps to Make String BalancedMedium"balance brackets with min swaps"Greedy: count unmatched pairs; answer = ⌈unmatched/2⌉
Number of Pairs of Interchangeable RectanglesMedium"count pairs with same aspect ratio"Normalise ratio as reduced fraction; count frequency pairs
Next PermutationMedium"next lexicographically greater permutation"Find rightmost descent; swap with next larger; reverse suffix
Permutation in StringMedium"does s2 contain a permutation of s1"Sliding window with sorted/frequency comparison

Complexity target: O(n log n) for merge-sort inversion count; O(n) for cycle-based swap count.

Recognition signal: "minimum swaps", "count inversions", "permutation", "cycles in rearrangement". The problem involves rearranging elements and measuring the cost.

Recommended Study Order

Week 1 — Sort and Apply
  Pattern 1 (Greedy): Merge Intervals, Meeting Rooms, Non-overlapping Intervals
  Pattern 2 (Custom): Sort Colors, Largest Number, Sort by Frequency

Week 2 — Selection and Search
  Pattern 3 (Binary Search): Two Sum II, 3Sum, H-Index
  Pattern 4 (QuickSelect): Kth Largest Element, Top K Frequent Elements

Week 3 — Advanced Sorting Techniques
  Pattern 5 (Counting): Group Anagrams, Maximum Gap, Top K Frequent Words
  Pattern 6 (Permutations): Count Inversions, Minimum Swaps, Next Permutation

Week 4 — Hard Problems and Mixed Patterns
  Pattern 1 (Hard): Task Scheduler, Car Fleet
  Pattern 4 (Hard): Wiggle Sort II, Find Median from Data Stream
  Pattern 6 (Hard): Couples Holding Hands
  Revisit any pattern that felt unclear

Problem-Solving Checklist for Sorting Problems

Before writing code, answer these questions:

1. Is sorting the solution or the first step?
   → Only sorting needed: Custom Comparator (Pattern 2)
   → Sort then linear scan: Sort and Scan (Pattern 1)
   → Sort then binary search: Sort and Search (Pattern 3)

2. What key (comparator) should elements be sorted by?
   → Natural order (ascending/descending)? → Default sort
   → By a computed property (freq, length, ratio)? → key= or lambda
   → By a pairwise comparison that is not transitive on single values? → custom comparator (e.g. Largest Number)

3. Does the problem have a special domain?
   → Small integer range [0, k]? → Counting Sort
   → Fixed-length integers/strings? → Radix Sort
   → Uniform float distribution? → Bucket Sort
   → Only 3 distinct values? → 3-way partition (Dutch National Flag)

4. Does the problem ask for a rank, not a sorted array?
   → k-th largest/smallest? → QuickSelect (O(n) avg)
   → Dynamic median? → Two heaps (O(log n) per insert)

5. Does stability matter?
   → Equal elements must preserve original order? → Use stable sort
   → Two-pass sort (secondary then primary key)? → Both passes must be stable

6. Edge cases for sorting problems:
   → Empty array or single element → typically trivial, but check
   → All equal elements → 3-way partition O(n); QuickSelect O(n)
   → Already sorted input → insertion sort / TimSort O(n); QuickSort O(n²) with naive pivot
   → Custom comparator that is not a valid strict weak ordering → undefined sort behaviour

Pattern Recognition Quick Reference

Signal in ProblemPatternTechnique
"merge overlapping intervals"Sort and ScanSort by start; linear scan merge
"minimum rooms / containers needed"Sort and ScanSort by start; min-heap of end times
"arrange numbers to make largest/smallest"Custom ComparatorString concatenation comparator
"sort by frequency"Custom ComparatorFrequency map + sort by (−freq, value)
"sort 0s, 1s, 2s in one pass"3-way partitionDutch National Flag, O(n) O(1)
"k-th largest/smallest"QuickSelectPartition, recurse into one half
"k most frequent"QuickSelect / HeapMin-heap of size k or QuickSelect on freq
"count inversions"Merge SortCount right-picks during merge step
"minimum swaps to sort"Permutation Cyclescycles → swaps = n − cycles
"group anagrams"Counting SortSort each string as key; group by key
"find all pairs summing to target"Sort + Two PointerSort; two pointers from both ends
"next permutation"PermutationFind rightmost descent; swap; reverse suffix
"maximum gap between sorted elements"Bucket Sortn+1 buckets; max gap spans buckets

Difficulty Progression Reference

StageCharacteristicsRepresentative Problems
FoundationSort built-in, linear scan on sorted resultMerge Intervals, Meeting Rooms, Sort Colors
Early IntermediateCustom comparator, two-pointer on sortedLargest Number, 3Sum, Non-overlapping Intervals
IntermediateQuickSelect, multi-key sort, countingKth Largest, Top K Frequent, Group Anagrams
Late IntermediateComplex greedy after sort, permutation basicsTask Scheduler, H-Index, Count Inversions
AdvancedMulti-step combination, cycle detection, hard selectionWiggle Sort II, Maximum Gap, Find Median Stream

Common Interview Topics by Company Focus

Product-based companies (FAANG-tier):

  • Merge Intervals and Meeting Rooms — canonical sort-and-scan problems
  • Kth Largest Element — QuickSelect is the expected approach
  • Find Median from Data Stream — two-heap design
  • Largest Number — custom comparator
  • Sort Colors — Dutch National Flag (frequently asked as a warmup)
  • Count Inversions — merge sort application
  • Top K Frequent Elements — heap vs QuickSelect discussion

Service-based and on-campus:

  • Sort Colors, Merge Intervals — fundamental
  • 3Sum, Two Sum II — sort + two-pointer standard
  • Kth Largest — basic QuickSelect
  • Group Anagrams — sort each string as key

Startup and mid-size tech:

  • Task Scheduler — real-world scheduling model
  • Reorder Log Files — practical multi-criteria sort
  • Minimum Swaps to Sort — permutation graph application
  • Next Permutation — permutation theory

Progress Tracking

After solving each problem, record:

  1. Pattern — which of the 6 patterns does this belong to?
  2. Sort key — what did you sort by and was a custom comparator needed?
  3. Step after sorting — linear scan, binary search, QuickSelect, or pure comparison?
  4. Stability needed — did equal elements need to preserve order?
  5. Edge case missed — empty input? All equal? Already sorted?
  6. Time to solve — target 20-25 minutes for medium problems under interview conditions

The decisive signal that pattern recognition is working: when you see "merge overlapping intervals" and immediately think "sort by start time, then linear scan" before reading the constraints — and when you see "k-th largest" and immediately think "QuickSelect, O(n) average, partition into one half" instead of "sort the whole array."

Suggested Quiz

A problem asks to find the k-th largest element in O(n) average time. Which technique applies?

1/6
Sorting Practice Problems