Teach / Coding
Coding
Data structures and algorithms, one problem per page, explained like you are ten.
The coding round is not a test of how many problems you have memorised. It is a test of whether you can take a problem you have seen before, in a shape you have not, and talk your way to the answer without panicking. These are the shapes. Learn them until you can draw them from memory.
Most lessons now end with a template: the generic code with the three parts that change written in capitals. The algorithms are not yours to invent at the whiteboard, someone did that years ago. Your job is to know the shape, understand why each line is there, and see which shape the new problem is wearing.
If your Python is rusty, read the Python cheat sheet first. It is the syntax and the gotchas, nothing else.
- Connected Components with Union-FindGiven a list of friendships, how many separate friend groups are there? Everyone starts as their own group. Each friendship merges two groups. A tiny structure with two operations answers it almost instantly, and it is what real systems use to track which servers are in which cluster.
- Subsets (Backtracking)Every possible group you can make from a set of items, including the empty group. For each item, choose to take it or leave it. That choose-then-undo rhythm is backtracking, and it is behind every 'all combinations' question.
- Sliding Window MaximumA window of size k slides along an array. For each position, what is the biggest number inside? A queue that throws away anything smaller than the newcomer answers it in one pass.
- Meeting Rooms: How Many Rooms Do You NeedA list of meetings with start and end times. What is the smallest number of rooms so nobody has to share? Sort by start, keep a heap of end times, and the size of the heap at its biggest is your answer.
- Unique PathsA robot in the top-left corner of a grid can only move right or down. How many ways to reach the bottom-right? Every cell is the sum of the cell above and the cell to its left. Fill the grid once.
- House RobberA street of houses with money inside. Rob any you like, but never two next to each other. At each house you choose: take it and skip the last one, or skip it and keep what you had. Two numbers, one walk down the street.
- Climbing StairsYou can take one step or two. How many ways to reach the top? The answer is Fibonacci, but the lesson is how to see that the answer to a big question is built from the answers to two smaller ones.
- Clone a GraphMake an exact copy of a graph where nodes point at each other, possibly in loops. The only new idea over a tree is a map from old node to new node, so you never copy anyone twice and never get stuck going round in circles.
- Min StackA stack that can tell you its smallest element instantly, at any moment. The answer is a second stack that remembers the smallest so far. Pay a little memory on every push to make one question free.
- Flood FillThe paint bucket tool. Click a square, and every connected square of the same colour changes too. Spread to the four neighbours, stop at a different colour or the edge. It is the same search you will use on every grid problem after this one.
- Validate a Binary Search TreeIs every node bigger than everything on its left and smaller than everything on its right? Checking only the children is the famous wrong answer. Carry a range down the tree instead.
- Merge IntervalsSort your meetings by start time, then walk the calendar once. If the next one starts before the current one ends, stretch. This one shape covers half of all scheduling questions.
- Invert a Binary TreeSwap left and right at every node. Three lines of recursion. The problem is a meme, but it is the cleanest test I know of whether you can trust a recursive call to do its job.
- Merge Two Sorted ListsTwo sorted queues. Keep taking the smaller front. A fake first node saves you from special-casing the start. This one shape is inside merge sort, log-structured databases, and every k-way merge you will ever write.
- IntervalsStart and end pairs: meetings, bookings, ranges. Sort them and the mess becomes a line you walk once. Sort by start to merge. Sort by end to pick the most that fit. The only question at each step is whether the next one starts before the current one ends.
- Detect a Cycle in a Linked ListTwo runners on a track. If the track is a loop, the fast one laps the slow one. If it is a straight road, the fast one falls off the end. No memory needed.
- Sliding Window, Fixed LengthFind the best run of exactly k items in a row. Do not re-add the window every time it moves. Add the one coming in, subtract the one going out. One pass, and the same trick works for sums, counts and letters.
- Binary SearchGuess my number between 1 and 100. Always guess the middle. Seven guesses, every time. The off-by-one errors are where the interview happens.
- Best Time to Buy and Sell StockPrices for each day. Buy once, sell once later, make the most money. Walk the days once, remembering only the cheapest price so far. The lesson is what to carry and what to throw away.
- StacksA pile of plates. You only ever touch the top one. That is enough to match brackets, undo edits, unwind nested things, and find the next bigger number for every item in one pass. Learn the three shapes and you have the whole family.
- Container With Most WaterVertical walls of different heights. Pick two that hold the most water between them. Start with a finger at each end and always move the shorter wall inward. Same shape as sorted Two Sum, and the lesson is why the move is safe.
- Move ZeroesPush every zero to the end of the array without changing the order of everything else. One finger marks where the next non-zero belongs. That finger is the whole idea behind two pointers.
- Two SumFind two numbers in a list that add up to a target. Unsorted list: remember what you have seen in a hash map. Sorted list: one finger at each end, walk them towards each other. Two tricks, one problem, and half of the array questions you will ever be asked.
- Reverse a Linked ListThree fingers, one loop. The first pointer problem every interviewer asks, and the first one where candidates lose the thread.