Why memorizing fails
There are thousands of problems and far fewer patterns. If you store each solution as a separate block of code, a new problem matches none of them. If you store the pattern and the one decision that changes from problem to problem, a new problem becomes a question you can answer: what is the decision here?
Study three related problems together
One problem cannot show you a pattern, because you cannot tell which lines are general. Three problems that share a pattern can. Solve them back to back and compare the code.
These three all use a sliding window: a range that grows on the right and shrinks on the left.
left = 0
for right in range(len(a)):
add a[right] to the window
while the window breaks the rule:
remove a[left] from the window
left += 1
update the answerThe first two differ in one line: the test in the while. The third breaks the template, and that is why it is the most useful of the three. It shows what the template assumes, which is that you want the longest valid window. Once you have seen where a pattern bends, you can apply it to a problem you have never met.
- Longest Substring Without Repeating Characters: the rule is broken when a character appears twice.
- Fruit Into Baskets: the rule is broken when the window holds more than two kinds of fruit.
- Minimum Size Subarray Sum: this one is reversed. The window shrinks while the sum is still large enough, and the answer is updated inside the inner loop, because the goal is the shortest window, not the longest.
Set a time limit before reading the solution
Thinking about a problem you cannot yet solve is where learning happens, but only up to a point. After that you are stuck, not learning. Set a limit, about 20 to 30 minutes for a medium problem.
When the limit ends, do not read the whole solution. Read only until you find the one idea you were missing, then close it and write the code yourself. Copying a finished solution trains typing. Finishing it from a hint trains solving.
Write three lines after every problem
A short note written straight after solving is what you will review later, so it has to hold the reasoning and not the code. Three lines are enough. For Fruit Into Baskets:
The third line is the most valuable. Your own mistakes repeat, and a list of them is a checklist nobody else can write for you.
- Signal: longest unbroken stretch of an array with a limit on what it may contain. That means a sliding window.
- Rule: at most two different values in the window. Track counts in a hash map, and shrink from the left while the map has three keys.
- Mistake I made: I lowered the count but did not delete the key when it reached zero, so the map still had three keys and the window kept shrinking.
Solve it again from a blank file
Reading your old solution and nodding is recognition. It feels like knowing, and it is not the same as being able to produce the code. The only honest test is an empty editor.
Solve the problem again after a few days, then again after a week or two, each time without looking. If you can rebuild it from the three-line note alone, you own it. If you cannot, the gap is exactly what to study next. Spacing the attempts out matters because the effort of recalling is what makes the idea stay.
Count what you can solve alone
The number of problems solved says little, because it counts problems you copied the same as problems you worked out. Track these instead.
Fifty problems you can rebuild are worth more in an interview than three hundred you once read.
- Problems solved without any hint.
- Problems rebuilt from a blank file a week later.
- New problems where you named the pattern within the first few minutes.
- Mistakes that showed up twice in your notes. Those are the ones to fix.