Module 6 · 4 lessons · about 7 hours 15 minutes
Sorting and binary search
How sorting works and why it costs n log n, binary search without off-by-one errors, binary search on the answer, and interval problems.
- Lessons
- 0/4
- Exercises
- 0/29
- Code
- 0/4
- Problems
- 0/15
- Checkpoint
- Not taken
Already comfortable with this? Take the checkpoint cold. If you pass, move on and come back only for what you missed.
- 1How sorting worksTwo sorts built from the walk you already know. Insertion sort grows a sorted front and costs O(n²). Merge sort splits a list into single items and merges them back in O(n log n). Then Python's own sort, and why so many solutions start by sorting.1 h 35 min
- 2Binary searchOn a sorted list, one look at the middle rules out half of what is left. Two small templates, one that finds a value and one that finds where a yes/no answer turns to yes, cover this whole family, rotated lists included.2 h
- 3Binary search on the answerSome questions ask for the smallest number that makes something possible, such as a speed, a capacity or a time. When a yes-or-no check on one guess switches from no to yes exactly once, binary search finds that number among a billion candidates with about 30 checks.1 h 30 min
- 4IntervalsAn interval is a pair [start, end]. In sorted order, the intervals that can overlap sit next to each other, so one walk with a small notebook merges them, inserts a new one, removes the fewest so none clash, or counts how many run at once.2 h 10 min
- Checkpoint11 questions. Pass with 80% to finish the module.
Problems for this module
The lessons teach some of these step by step (each says which). The rest are practice: try each in its target time, and solved problems come back for spaced re-solves.
- mediumSort a list with merge sort25 min
- easyAttend every meeting15 min
- easyBinary search15 min
- easySearch insert position15 min
- mediumSearch a rotated sorted list25 min
- mediumMinimum of a rotated sorted list25 min
- mediumSearch a sorted grid25 min
- easyFirst bad version15 min
- mediumKoko eating bananas25 min
- mediumSmallest ship capacity25 min
- hardSplit into k pieces40 min
- mediumMerge overlapping intervals25 min
- mediumInsert an interval25 min
- mediumFewest intervals to remove25 min
- mediumMeeting rooms needed25 min