How to Learn Algorithms Efficiently
For job seekers applying for technical positions, whether it's campus recruitment or social recruitment, and whether it's a written test or an interview, algorithm assessment has become almost standard.
Campus recruitment candidates, in particular, should pay close attention to algorithms because, without work experience, there's little to ask about real project experience. If asked about standard questions, there are fixed answers, leaving little room for creativity. Moreover, with tools like ChatGPT becoming so advanced, the value of memorizing standard answers has decreased.
Algorithms are the only variable with significant room for assessment, which is why many reputable companies focus on them during campus recruitment. They are well-suited for screening potential hires.
I recommend that campus recruitment candidates start preparing early. With ample time, they can go through the content on this site to systematically master data structures and algorithms, which can alleviate many challenges when changing jobs in the future.
For social recruitment, work experience is undoubtedly the most important factor. Good companies also test algorithms, but they generally only reach a medium difficulty level and are not overly difficult.
Therefore, this site offers a Quick Guide specifically for algorithm tests, helping readers who are short on time to address urgent needs.
Here, I will not discuss the practical significance of algorithms in the workplace but will focus on a scientific analysis of algorithm practice problems. I will provide efficient solutions tailored to different readers, helping everyone improve their algorithm skills so that algorithms are no longer an obstacle in job hunting.
How Algorithms Are Assessed in Job Interviews
In the context of applying for technical positions, the term "algorithm" generally encompasses two major components: data structures and algorithms, each of which is assessed differently.
Data Structures are relatively straightforward and are typically examined during interviews. For instance, an interviewer might ask you about the principles of a hash table, why its operations such as insertion, deletion, and lookup are efficient at , along with a series of follow-up questions. These questions are similar to rote questions, and as long as you have studied and understood them thoroughly, they are usually easy to answer.
The introductory sections of our site cover the principles and implementations of all common data structures. If time permits, you can try implementing them yourself to deepen your understanding. However, if time is limited, thoroughly understanding the principles is sufficient, and you generally won't need to manually implement data structures.
Algorithms are often the more challenging part, commonly referred to as "hand-tearing algorithm problems," where you are given a problem and must write a solution that passes the evaluation system.
Hand-tearing algorithm problems are guaranteed to appear in written tests, as they are a strict requirement; failing to solve them means you won't even have a chance for an interview. In most interviews, the interviewer may also ask you to solve algorithm problems on the spot. However, this is relatively easier because you can always discuss with the interviewer if you encounter difficulties.
Apart from the introductory sections on data structures, all other content on our site focuses on techniques for solving algorithm problems. It is crucial to practice solving problems yourself to truly master these algorithmic skills.
Key Points in Solving Algorithm Problems
The key to solving algorithm problems lies in building your own problem-solving methodology, often referred to as a "framework thinking" on this site.
Specific problems have little value, and fleeting techniques are not valuable either. A systematic, consistently reproducible methodology is what holds true value.
This site is essentially a culmination of my framework thinking, rather than a mere collection of solutions. Learning this methodology is worth the time and money you invest.
When faced with a new problem, this methodology can offer you starting points, guide you through step-by-step optimizations, and ultimately help you write the solution code.
Algorithms are much like math exams; rote memorization is ineffective. The lack of ideas when encountering new problems is fundamentally because you have not mastered your own methodology.
Why This Site Can Help You Learn Quickly
In high school, you might have seen students who took meticulous notes yet still struggled with exams, and those who seemed to sleep through classes but excelled. The secret of those who succeed is not in having perfectly organized notes but in possessing a strong "ability to abstract."
Take programming as an example. A parent class can produce several subclasses, and a subclass can have countless instances.
If you get lost in the details of each instance, you'll be overwhelmed. Even if you memorize a thousand instances, there will be ten thousand more waiting for you. However, if you can step back from the details and see the parent class through the instances, ten thousand instances appear as just one to you. This is the power of abstraction.
In the introductory section of Framework Thinking for Learning Data Structures and Algorithms, I mentioned two key points:
There are essentially only two types of data structures: arrays and linked lists. Everything else is a derivative of these two.
Algorithms are essentially brute-force. The fancy algorithm names are just terms; what they do is essentially brute-force.
With these two points as guiding principles, data structures and algorithms are not as difficult as they seem. You might have found them challenging before because you didn't approach them through these perspectives.
Will having these two points make you invincible? Not quite. The next step is to concretize these concepts.
For instance, the brute-force method varies with different problems. You need to summarize and organize these scenarios by turning the repetitive parts into processes and frameworks. These frameworks become your problem-solving methodologies.
The core framework sections of this site summarize several frameworks for common algorithm problems.
Are frameworks alone enough to make you invincible? Still not quite. The next step is to internalize these frameworks through continuous practice so you can use them instinctively.
The "Intensive Practice" sections in this site's directory serve this purpose by providing extensive practice on the same set of frameworks, helping you develop muscle memory and truly own your problem-solving methodologies.
In summary, this is the scientific approach to problem-solving that I have developed. All content on this site revolves around this process.
Common Pitfalls in Learning Algorithms
Pitfall One: Misguidance and Wasted Time
Beginners are especially prone to being misled, thinking that algorithms are very advanced and require a strong mathematical foundation, or that one must finish reading "Introduction to Algorithms" to truly get started. This creates an inherent fear of algorithms.
I have firsthand experience with this issue. When I first started learning algorithms, I searched through various materials and forums. I recall many so-called experts would advise reading "Introduction to Algorithms" and then recommend a bunch of English courses, insisting that the English versions are essential because the translations are not good.
It's like asking someone how to lose weight, and they tell you to do 100 push-ups and 200 pull-ups every day, claiming it works because that's how they did it!
Honestly, if I could manage those, why would I even ask? But if you think about it, what's the mindset behind such advice? They don't really care about your needs; they just want to show off: "Look what I've done, isn't it impressive? You can't do it, can you?"
Many professional algorithm books are indeed valuable, with substantial content, but their target audience differs. Most people are just looking for a tech job, and reading those books is not cost-effective.
What's more cost-effective? Directly practicing problems. The direction must be clear: practicing problems is the main path, and everything else is supplementary. If you dabble in a bit of this and a bit of that, you might feel knowledgeable, but end up realizing none of it helps during a test. Then who can you blame?
Pitfall Two: Greed for Quantity and Tricks
What does "greed for quantity" mean? Some readers might fall into the trap of pursuing sheer volume. For instance, some training institutions flaunt having hundreds of gigabytes of materials, boasting comprehensive content.
In my view, having a large amount of content is not an advantage; it is rather a disadvantage. To be honest, even if these hundreds of gigabytes of materials are really good, would you actually spend the time and patience to go through them, or would they just sit idly in your bookmarks gathering dust?
Especially for algorithms, which lean towards practical application, merely reading is not enough. If you don't practice hands-on, it remains just theoretical talk.
What does "greed for tricks" mean? It's easy to be attracted by the brilliant solutions shared by experts in the LeetCode comment section. However, I want to say that before developing your own systematic thinking, there is no need to seek multiple solutions for a single problem.
Pursuing multiple solutions is certainly beneficial, but if you haven't yet mastered your own problem-solving methodology, it's better to honestly practice the structured thinking provided here.
As you advance, you will realize that simplicity is key; algorithms are essentially brute-force. All the content here revolves around this concept. In most cases, writing code according to a structured framework, adapting as you go, and optimizing step by step will lead you to the optimal solution. These solutions are just as efficient as those flashy algorithms. Embrace consistency amidst change—why not?
Pitfall Three: Lack of Strategy in Problem Solving
Once you've determined that the main approach is practicing problems, the next pitfall is doing so without a strategy—randomly tackling problems without focus. With thousands of questions on LeetCode, are you planning to solve and memorize all of them?
I don't deny that hard work can lead to breakthroughs. For example, if you manage to solve a thousand problems, you'll definitely gain some insights. However, the issue is that I have a clever method that can make one question worth ten. Would you like to learn it? Of course you would. It's quite simple: don't focus on quantity when practicing problems, but rather on summarizing and abstracting them. The ultimate goal is to develop a framework of thinking.
In essence, most algorithm problems are variations of the same themes. Once you understand the core principles, you can apply them to different scenarios with ease.
Why do I emphasize this framework thinking? Because it fundamentally solves the problem of not knowing where to start with new questions.
For example, if you've mastered this framework thinking, any question can be approached with a structured thought process:
What is this problem asking us to do? Ah, it's about manipulating strings. A string is essentially an array, so this problem likely involves array-related algorithm techniques.
What are the algorithm techniques for arrays? You should already have them in mind: Binary Search, Fast and Slow Pointers, Left and Right Pointers, Sliding Window, Prefix Sum Array, Difference Array, among others.
What are the scenarios where binary search is applicable? What about difference arrays? Think through each one, and if one doesn't work, try another. You'll eventually find a relatively reliable approach.
Let's say you determine that using a sliding window is suitable for the problem. An average person, when told to use a sliding window, might struggle to write even a line of code. But you're different. You know the framework for sliding windows, so you first write the framework and then fill it in with code.
How do you fill it in? It's simple. The core framework of sliding windows has been summarized. When using a sliding window, you should ask yourself three questions. Answer these questions, and you'll have solved the problem and submitted the code successfully. Isn't that satisfying?
Enjoyable, right? Then I'll give you 100 sliding window exercises. You'll solve them by following the same pattern, making minor adjustments. Does the algorithm still seem difficult to you now?