Worked problems
Each lesson takes one problem from the slow solution to the fast one, in Python, and shows why the fast one is correct. Every lesson ends by changing one rule of the problem and asking whether the solution still works.
How each lesson works
Every lesson follows the same steps, so you can compare techniques side by side:
- The problem, with examples.
- The slow solution that obviously works, and what makes it slow.
- The key observation that removes the slow part, and what it depends on.
- The fast solution, with a step-by-step trace.
- Why it works, in a form you could check yourself.
- Cost, edge cases, and where it breaks, including a small input that defeats it.
A collapsed section at the end has related problems, random tests against the slow solution, and an exercise to rebuild the solution from memory.
Want to try first? Each problem opens in the practice workspace, where your Python runs against the lesson's tests. To read the solution without trying first, open its Solution tab.
The lessons
- Two Sum · Easy
- Group Anagrams · Medium
- Two Sum II · Medium
- Search Insert Position · Easy
- Shortest subarray reaching a sum
- Subarrays summing to k
- Daily Temperatures · Medium
- Sliding Window Maximum · Hard
- Koko Eating Bananas · Medium
- Valid Parentheses · Easy
- Kth Largest Element · Medium
- Merge k Sorted Lists · Hard
- Valid Anagram · Easy
- Longest Substring Without Repeating Characters · Medium
- Move Zeroes · Easy
- Maximum Subarray · Medium
- Merge Intervals · Medium
- Reverse Linked List · Easy
- Maximum Depth of Binary Tree · Easy
- Number of Islands · Medium
- House Robber · Medium
- Trapping Rain Water · Hard
- Linked List Cycle · Easy
- Balanced Binary Tree · Easy
- Diameter of Binary Tree · Easy
- Validate Binary Search Tree · Medium
- Course Schedule · Medium
- Redundant Connection · Medium
- Network Delay Time · Medium
Pairs worth reading together
- Shortest subarray reaching a sum and Subarrays summing to k look almost the same. The first allows only positive numbers, so a sliding window works. The second allows negative numbers, which breaks the window; it needs prefix sums and a hash map.
- Daily Temperatures and Sliding Window Maximum both throw away candidates that can never win. The second also lets candidates expire, which turns a stack into a deque.
- Two Sum is a good first lesson. Its trick, rewriting a condition on a pair, comes back in the prefix-sum lesson.
How to study a lesson
- Read the problem, then stop. Write the slow solution and say what makes it slow before you read step 2.
- Finish it yourself. After the key observation, try to write the fast solution before reading it.
- Come back in a few days. Rebuild the solution from a blank page. Recognizing an explanation is not the same as being able to produce it.
Every code block is plain Python 3. Our tests run each one and compare the fast solution with the slow one on thousands of random inputs. Passing tests make a bug unlikely, but they don't replace the argument in step 5.
The pattern chapters cover many more techniques in shorter form.