The hardest part of solving algorithms
If you have been solving algorithms questions for a while, you will notice specific patterns and topics are just more challenging than usual. Before we dive deeper into these topics, I understand everyone has their own most challenging algorithm topics out there. However, I am here stating my own opinion on what I have seen so far that I feel are the most challenging algorithms topics. I know I am not alone, and I believe my intuition aligns with many folks out there.
Dynamic Programming
Dynamic Programming (DP) is one of the most well-known topics that everyone struggles with. It feels like there is no shortcut way of making it simple. It requires a lot of practice and repetition of solving those types of questions to get good at it. Dynamic programming is a method for solving a complex problem by breaking it down into a collection of simpler subproblems, solving each subproblem just once, and storing their solutions. In other words, it is a way of approach and pattern solving problems, breaking it down into smaller pieces, and reducing duplication of those smaller pieces by storing them somehow. Dynamic Programming is a trendy challenging topic in leetcode, where everyone on the discussion page is talking about it.
Optimizing
You will be surprised how hard it is to optimize the time and space complexity of your code. This topic is one of the most iconic topics among technical interviews. Sometimes getting the right answer is not hard, but making the code more efficient and abstract are two different stories. One example below, as you can see, the question is simple to answer if it is without the big-o notation constraint. Nevertheless, if it evolves big-o notation, which you need to optimize your code run time complexity, then it might pause you for a bit and think.
Getting the right answer with the Brute Force way for solving this problem is not hard at all, but with O(n) time complexity requires time and effort to solve. Here is another example.
Implementation
There are two parts of problem-solving in algorithm questions. The first is coming up with the algorithm, and the second is implementing the algorithm. Coming up with algorithms could be easy sometimes. With the understanding of data structures, knowledge could support the approach of coming with algorithms for the problem. On the other hand, implementing the algorithm could be difficult. Many hard questions in leetcode give you hints on what kind of data structures/topics to solve the problem with, yet you are still struggling to solve them. Implementing hard algorithms problems is hard, even when you understand the question well and know what kind of data structure/topics to approach them. What’s even more challenging is algorithms questions that rely on massive implementation, such as questions you can easily find in competitive programming contests and high school/college programming contests. There is no doubt the difficulties of these problems are not a match.
Math Tricks
Have you heard many folks say that you need to know some math to understand how to program? Well, some specific algorithms questions require you to understand some math theory to solve them, but in terms of programming, there are two completely different stories. Nowadays, there is Google, where you can search for beautiful theories and study from them. However, specific math theories are hard and tedious, not as simple as everyone learns in high school Pythagoras’ Theorem.
To name a few, number theory, modular arithmetic, game theory, probability theory, graph theory, computational geometry, permutations, combinations, etc. There are particular algorithm problems straight up asking math topics; all you need to do is a study and implement them with the theoretical knowledge approach.
You can find these types of questions a lot among competitive programming contests, Olympiad among high schools (IOI), and college programming contests (ICPC). Although there are also challenging problems in leetcode that evolve with math topics, however, the one that is on coding competition is not comparable. Check out here and here for some examples that require understanding some mathematical concepts to solve the problem.
Conclusion
So there you go! These are the topics and patterns that I found that are difficult in solving algorithm questions. Of course, I believe everyone has a unique experience of having different categories of the hardest things to solve in algorithms. If you find something challenging topics or patterns in solving algorithms that are not listed here, please let me know. As always, I hope you enjoy my article and thanks for taking the time to read.