How to Practice
No matter if you are following the full guide for beginners or a quick mastery outline, there are always some exercise sections right after the explanation of the algorithm framework. The main goal is to help you truly learn the algorithm framework, so you can use it by yourself when facing new problems.
Many readers often ask, "Why do I understand everything when reading the explanation, but I cannot solve problems on my own?" The answer is simple: you have not fully learned and practiced it yourself.
These exercises are of medium difficulty and can be solved directly by applying the framework. By practicing many similar problems, you will quickly internalize the algorithm patterns. This is why the exercise section is very important.
How to Use the Exercise Section
Here is a method for practicing with exercises. I have tested this myself and it works well.
For example, after reading the Core Sliding Window Algorithm Framework, you should also try the related set of exercises: Exercise: Classic Sliding Window Problems.
First, make sure to study the Core Sliding Window Algorithm Framework carefully.
When doing the sample problems, you can follow the ideas in the article and practice by writing code step by step. But after that, you must make sure you can solve at least one new sliding window problem all by yourself.
Don’t just feel like you have learned it. You have to test yourself in this way to make sure you really know it.
The exercises in Exercise: Classic Sliding Window Problems are good for practicing. At first, do not look at my solutions—try to solve them on your own. If you cannot solve the first problem, don’t get stuck there, you can skip to the next problem.
Pushing yourself to think through these questions might be a bit hard, but it is a necessary step. By making mistakes and trying different things, you will learn the details of the framework.
All these exercise problems are carefully chosen and fit the framework. You should be able to solve at least one of them independently. If you solve one, you pass! For the rest, compare your ideas to the solutions I give, see where you got stuck, and think about how to improve.
How to Review the Exercises
Review is important. I suggest that after you finish an exercise section, you should come back and do the problems again after about 3 days.
Since you have already done them once, try not to look at my solutions the second time—solve them yourself. If there are ones you cannot solve again, check the answer, and then, a few days later, try again. Repeat until you can solve them all.
Do Not Memorize Solutions
Algorithms are not something you can just memorize and copy. Do not try to memorize every solution—it doesn’t help. Focus on understanding the ideas, not memorizing the code.
For example, do not just remember that you used <= last time and copy it this time when reviewing. That is not helpful.
Forgetting the code is okay, even good. This is your chance to analyze and think as you write. Ask yourself: Why should I use <=? What happens if I use <? You must be able to explain your reasoning. This is what true reviewing means, not just remembering things without thinking.