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.
| Problem | Difficulty | Recognition Clue | Key Insight |
|---|---|---|---|
| Merge Intervals | Medium | "merge overlapping intervals" | Sort by start; scan and merge when next.start ≤ current.end |
| Meeting Rooms | Easy | "can attend all meetings without overlap" | Sort by start time; check if any consecutive pair overlaps |
| Meeting Rooms II | Medium | "minimum conference rooms needed" | Sort by start; min-heap of end times — greedy room assignment |
| Non-overlapping Intervals | Medium | "minimum removals to make non-overlapping" | Sort by end time; greedy keep earliest-ending intervals |
| Minimum Number of Arrows | Medium | "minimum arrows to burst all balloons" | Sort by end; greedy one arrow per overlapping group |
| Car Fleet | Medium | "cars that arrive together form a fleet" | Sort by position (descending); track when faster car is caught |
| Task Scheduler | Medium | "minimum intervals to finish tasks with cooldown n" | Sort by frequency; max-heap fills cooldown slots |
| Hand of Straights | Medium | "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.
| Problem | Difficulty | Recognition Clue | Key Insight |
|---|---|---|---|
| Largest Number | Medium | "arrange integers to form largest number" | Custom comparator: compare ab vs ba as strings |
| Sort Characters by Frequency | Medium | "sort by frequency descending" | Build frequency map; sort by (−freq, char) or max-heap |
| Sort Array by Parity | Easy | "even numbers first, odd last" | Partition in-place: two-pointer even/odd separation |
| Sort Array by Parity II | Easy | "even indices have even elements" | Two-pointer: fill even indices from even pool, odd from odd |
| Rank Teams by Votes | Medium | "rank candidates by vote positions" | Custom comparator: compare vote counts at each position |
| Reorder Data in Log Files | Medium | "letter-logs before digit-logs, letter-logs sorted" | Stable sort: key by (log-type, content, identifier) |
| Wiggle Sort | Medium | "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 Difference | Easy | "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).
| Problem | Difficulty | Recognition Clue | Key Insight |
|---|---|---|---|
| Two Sum II (sorted array) | Easy | "sorted array, find pair summing to target" | Two-pointer on sorted array — O(n) after sort |
| 3Sum | Medium | "find all triplets summing to zero" | Sort; for each element binary search or two-pointer for pair |
| 4Sum | Medium | "find all quadruplets summing to target" | Sort; nested loops + two-pointer for remaining pair |
| Find Target in Rotated Array | Medium | "binary search in rotated sorted array" | Sort first or use rotated binary search |
| H-Index | Medium | "maximum h papers with ≥ h citations" | Sort citations descending; scan until citations[i] < i+1 |
| Search a 2D Matrix | Medium | "row-sorted, globally sorted matrix" | Treat as flat sorted array; binary search with row/col mapping |
| Count Pairs With Absolute Diff ≤ K | Easy | "count pairs (i,j) with | arr[i]-arr[j] |
| Missing Ranges | Easy | "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.
| Problem | Difficulty | Recognition Clue | Key Insight |
|---|---|---|---|
| Kth Largest Element in Array | Medium | "k-th largest, O(n) preferred" | QuickSelect: partition, recurse into one side only |
| Find Median from Data Stream | Hard | "dynamic median as elements arrive" | Two heaps: max-heap (lower half) + min-heap (upper half) |
| Kth Smallest in Sorted Matrix | Medium | "k-th smallest in row/col sorted matrix" | Binary search on value + count; or min-heap |
| Wiggle Sort II | Hard | "nums[0] < nums[1] > nums[2] < ..." | QuickSelect to find median; 3-way partition; virtual indexing |
| Top K Frequent Elements | Medium | "k most frequent elements" | Frequency map + QuickSelect on frequencies, or min-heap |
| K Closest Points to Origin | Medium | "k points with smallest distance" | QuickSelect on distance², or max-heap of size k |
| Minimum Cost to Hire K Workers | Hard | "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.
| Problem | Difficulty | Recognition Clue | Key Insight |
|---|---|---|---|
| Sort Colors (0s, 1s, 2s) | Medium | "three distinct values in one pass" | Dutch National Flag: 3-way partition, O(n) O(1) |
| Maximum Gap | Hard | "maximum difference between successive elements" | Bucket Sort: n+1 buckets, max gap crosses bucket boundaries |
| Top K Frequent Words | Medium | "k most frequent strings" | Frequency map + bucket sort by frequency → O(n) |
| Group Anagrams | Medium | "group strings that are anagrams" | Sort each string as key → group by sorted key |
| Valid Anagram | Easy | "are two strings anagrams" | Frequency count of each character; compare counts |
| Find All Anagrams in String | Medium | "find all anagram starting positions" | Sliding window + character frequency comparison |
| Minimum Deletions to Make Array Beautiful | Medium | "alternating parity after minimal deletions" | Count sort by parity category |
| Sort Array by Increasing Frequency | Easy | "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.
| Problem | Difficulty | Recognition Clue | Key Insight |
|---|---|---|---|
| Count Inversions | Medium | "pairs (i,j) where i<j but arr[i]>arr[j]" | Merge sort: count right-element picks during merge phase |
| Minimum Swaps to Sort Array | Medium | "minimum swaps to sort using position mapping" | Build permutation graph; swaps = n − number_of_cycles |
| Couples Holding Hands | Hard | "minimum swaps so each couple sits together" | Permutation cycles on couple positions |
| Minimum Number of Swaps to Make String Balanced | Medium | "balance brackets with min swaps" | Greedy: count unmatched pairs; answer = ⌈unmatched/2⌉ |
| Number of Pairs of Interchangeable Rectangles | Medium | "count pairs with same aspect ratio" | Normalise ratio as reduced fraction; count frequency pairs |
| Next Permutation | Medium | "next lexicographically greater permutation" | Find rightmost descent; swap with next larger; reverse suffix |
| Permutation in String | Medium | "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 Problem | Pattern | Technique |
|---|---|---|
| "merge overlapping intervals" | Sort and Scan | Sort by start; linear scan merge |
| "minimum rooms / containers needed" | Sort and Scan | Sort by start; min-heap of end times |
| "arrange numbers to make largest/smallest" | Custom Comparator | String concatenation comparator |
| "sort by frequency" | Custom Comparator | Frequency map + sort by (−freq, value) |
| "sort 0s, 1s, 2s in one pass" | 3-way partition | Dutch National Flag, O(n) O(1) |
| "k-th largest/smallest" | QuickSelect | Partition, recurse into one half |
| "k most frequent" | QuickSelect / Heap | Min-heap of size k or QuickSelect on freq |
| "count inversions" | Merge Sort | Count right-picks during merge step |
| "minimum swaps to sort" | Permutation Cycles | cycles → swaps = n − cycles |
| "group anagrams" | Counting Sort | Sort each string as key; group by key |
| "find all pairs summing to target" | Sort + Two Pointer | Sort; two pointers from both ends |
| "next permutation" | Permutation | Find rightmost descent; swap; reverse suffix |
| "maximum gap between sorted elements" | Bucket Sort | n+1 buckets; max gap spans buckets |
Difficulty Progression Reference
| Stage | Characteristics | Representative Problems |
|---|---|---|
| Foundation | Sort built-in, linear scan on sorted result | Merge Intervals, Meeting Rooms, Sort Colors |
| Early Intermediate | Custom comparator, two-pointer on sorted | Largest Number, 3Sum, Non-overlapping Intervals |
| Intermediate | QuickSelect, multi-key sort, counting | Kth Largest, Top K Frequent, Group Anagrams |
| Late Intermediate | Complex greedy after sort, permutation basics | Task Scheduler, H-Index, Count Inversions |
| Advanced | Multi-step combination, cycle detection, hard selection | Wiggle 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:
- ›Pattern — which of the 6 patterns does this belong to?
- ›Sort key — what did you sort by and was a custom comparator needed?
- ›Step after sorting — linear scan, binary search, QuickSelect, or pure comparison?
- ›Stability needed — did equal elements need to preserve order?
- ›Edge case missed — empty input? All equal? Already sorted?
- ›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."
A problem asks to find the k-th largest element in O(n) average time. Which technique applies?