One idea at a time, then the problems that use it
A path from counting digits to graphs, numbered so you always know where you are. Each lesson explains a single technique, shows it running step by step, and hands you the problems where it is the right answer. C, C++, Java and Python are only ways of talking to a computer. The thinking is what carries across.
36 lessons, in order. Start at one, or start where you are stuck.
Your learning map
Sign in to keep your progress on the map.
First steps
steps 1 to 6How a solution runs here
Beginner8 min readWhat the judge does with your code: you write one function, it reads the input, calls you and checks what you return. The same problem in C, C++, Java and Python.
0 of 3 practice problems solved
Values, types and the numbers that overflow
Beginner10 min readWhole numbers and how big they get, dividing without fractions, remainders, doubles that are almost right, and text in four languages.
0 of 3 practice problems solved
Decisions and loops
Beginner10 min readif and else, for and while, stopping early, the off by one error, and why a loop inside a loop is too slow for large inputs.
0 of 3 practice problems solved
Functions, helpers and what happens to arguments
Beginner9 min readSplitting a solution into small named helpers, which arguments a function can change for its caller, and returning more than one value.
0 of 3 practice problems solved
Arrays and lists in everyday code
Beginner10 min readFixed and growable arrays, returning one to the judge, sorting with the built-in sort, and building grids without the classic Python trap.
0 of 3 practice problems solved
Maps and sets in everyday code
Beginner9 min readFinding things by value rather than position: sets, maps and counting in C++, Java and Python, and the array tricks that stand in for them in C.
0 of 3 practice problems solved
Numbers
step 7Arrays and hashing
steps 8 to 12Where your data actually lives
Beginner8 min readMemory is one long numbered street. Almost every cost in this course comes from that one fact, so it is worth ten minutes before anything else.
0 of 4 practice problems solved
Arrays: what is cheap and what is not
Beginner9 min readFour operations, four different costs. Knowing which is which turns most array problems from guesswork into arithmetic.
0 of 10 practice problems solved
Hash tables: how a lookup can skip the search
Beginner11 min readTurn a key into a number, use that number as an address. The whole trick, plus what happens when two keys collide.
0 of 4 practice problems solved
Trading memory for time with a hash map
Beginner12 min readWhy a second pass beats a nested loop, and how remembering what you have seen turns a quadratic search into a single sweep.
0 of 12 practice problems solved
Carrying an answer forward: Kadane's algorithm
Intermediate12 min readThe best subarray ending here is either this element alone or this element added to the best one ending just before it. That single sentence is the algorithm.
0 of 5 practice problems solved
Two pointers and windows
steps 13 to 14Two pointers: replacing a nested loop with a single pass
Beginner10 min readTwo indexes walking one array, never going backwards. Turns a great many quadratic solutions into linear ones.
0 of 13 practice problems solved
Sliding windows: fixed size, then variable
Intermediate14 min readA window is a stretch of a list you keep looking at while both its ends move right. Get the fixed-size version first, because the variable one is the same idea with a rule instead of a length.
0 of 8 practice problems solved
Recursion
step 15Sorting
step 16Searching
steps 17 to 18Binary search, and getting the boundary right
Beginner14 min readHalving the search space is the easy part. The hard part is deciding what to return when the value is not there.
0 of 7 practice problems solved
Binary search on the answer, when nothing is sorted
Intermediate13 min readA dozen questions that look nothing alike are one question: guess an answer, check it in a single pass, and use the fact that answers come in a run of no followed by a run of yes.
0 of 4 practice problems solved
Linked lists
steps 19 to 20Linked lists: giving up indexing to buy insertion
Beginner10 min readValues that do not sit next to each other, each one holding the address of the next. The exact opposite trade from an array.
0 of 5 practice problems solved
Loops in a linked list: two walkers, no extra memory
Intermediate13 min readA list that points back at itself has no end to find. One slow walker and one fast one settle whether there is a loop, how long it is, where it starts and how to cut it, using two variables and nothing else.
0 of 5 practice problems solved
Stacks and queues
steps 21 to 23Stacks and queues: choosing which end to work from
Beginner9 min readTwo structures that differ by one decision: when you take something out, is it the newest or the oldest?
0 of 11 practice problems solved
Stacks: when the most recent thing is the thing that matters
Beginner10 min readNesting is the signal. When the correct partner for what you are looking at is always the most recent unmatched item, you want a stack.
0 of 3 practice problems solved
Monotonic stacks: the nearest bigger thing, in one pass
Intermediate12 min readA whole family of problems asks for the nearest larger or smaller value. They all fall to a stack kept in sorted order, and they all cost one pass.
0 of 5 practice problems solved
Trees
steps 24 to 26Trees: one thing above, several below
Beginner10 min readA linked list that branches. Everything about trees comes from that, including why searching a balanced one is so fast.
0 of 8 practice problems solved
Traversals: four orders, and which question each answers
Beginner12 min readPreorder, inorder and postorder differ by one line's position. Level order differs by using a queue instead of recursion, and that single change is the whole idea behind breadth-first search.
0 of 5 practice problems solved
Search trees: one rule, and everything that follows from it
Intermediate13 min readA search tree keeps smaller on the left and larger on the right. Every operation on one, and every trap, comes from taking that rule seriously across the whole tree rather than one node at a time.
0 of 3 practice problems solved
Heaps
steps 27 to 28Heaps: when you only ever want the next one
Intermediate10 min readA tree that gives up on full sorting and keeps only one promise: the smallest is on top. That is cheaper, and often it is all you need.
0 of 4 practice problems solved
Two heaps: keeping the middle in view
Advanced16 min readA heap gives you the extreme of a set in constant time. Point two of them at each other and the extreme becomes the median.
0 of 3 practice problems solved
Graphs
steps 29 to 31Graphs: things, and the connections between them
Intermediate11 min readDrop the rule that every node has one parent and you have a graph. Two ways to walk one, and the choice decides what you can prove.
0 of 4 practice problems solved
Breadth first and depth first: the same loop, a different container
Intermediate13 min readBoth walks visit everything reachable. Only one of them answers questions about shortest, and the difference between them is which end you take things off.
0 of 4 practice problems solved
Topological order: doing things in an order that works
Intermediate16 min readCourses with prerequisites, build steps, task lists. If the dependencies contain a cycle there is no valid order, and finding that out is the same algorithm.
0 of 3 practice problems solved
Backtracking
steps 32 to 33Backtracking: choose, explore, un-choose
Intermediate15 min readWhen a problem asks for every arrangement rather than one number, you need a way to try each choice and take it back. Three lines do it, and one of them is the line everybody forgets.
0 of 6 practice problems solved
Tries and backtracking: searching many words at once
Advanced18 min readSearching a grid for one word is backtracking. Searching for a thousand is backtracking with the dictionary turned inside out.
0 of 3 practice problems solved
Dynamic programming
steps 34 to 35Dynamic programming without the mystique
Advanced20 min readA state, a recurrence, and an order to fill it in. Longest increasing subsequence, first the honest way and then the fast one.
0 of 6 practice problems solved
Grid DP: when the table is the same shape as the problem
Intermediate12 min readA grid you can only cross one way is where dynamic programming is easiest to see, because the table of answers has the same shape as the board and you can watch it fill.
0 of 3 practice problems solved