How to get started with algorithms to be a better developer13 Feb 2017
As a developer, for a long time I resisted the idea that solving algorithmic challenges was something worth doing. I have started and stopped learning about sorting and searching and trees and graphs a countless number of times, every time with the same outcome — I was back to the projects where none of these algorithms were applicable, and the knowledge quickly faded away due to a lack of use.
But recently I decided to give it another try, and this time was a success. I used a new approach that helped me to stay on track long enough to see the real benefit of solving algorithmic challenges— it makes you think more creatively. It lets you see familiar problems from different angles and come up with better solutions. It builds the confidence that you can tackle new and unfamiliar problems. And as a result, it makes you a better developer.
So I wanted to share the principles that can be useful if you want to improve your knowledge of algorithms but intimidated by theory-heavy books. Here are the principles I follow:
Start with real examples, not the theory. It’s easy to get bogged down with lots of technicalities of the algorithms in the beginning and give up. Instead, find a programming task that you feel you can solve, then do it. It’s not important that your solution may be not the most efficient — the point is that you solved it.
If the problem seems too difficult, go and find the solution for it on the internet, ideally, written in the programming language you don’t know. Almost every algorithmic challenge has been solved and tested, and solutions are available online. They come with a textual explanation, too, so read it and go learn about any new concepts. This is a good time to get familiar with the theory, now that you have a context of an actual problem in mind.
Very important: resist the urge to look into the actual solution of the problem — that completely defeats the purpose.
If you get stuck, find a ready-made solution that solves a part of the problem, then try to re-implement that solution yourself. It’s very important that you don’t just read its source code, but actually type it in. In the process, you will better understand why things work the way they do, which may lead you just to the insight you need for solving your original problem.
If you’ve struggled with the problem for hours but still haven’t figured out the solution, it’s alright to look into the answer. There’s still a way to make it useful — once you’ve studied the solution and understood how different algorithms and data structures make it work, rewrite it in your favorite language to solidify your understanding. As with many other things, you learn algorithms by doing, not by reading.
A side note: beware of “bad” problems. Whereas “good” programming tasks can be solved using common logic and one or two well-known algorithms, there are some out there that require you to know an obscure math formula or never-used-again approach. Don’t feel bad when you fail to solve a “bad” problem, there’s little to gain from it. Just recognize it as “bad” and move on to the next one.
To put this approach to work, here are a few great resources that I used:
- Advent of Code 2015 and 2016 — these are my favorite ones. It’s easy to get started since the first challenges require almost no specific knowledge of algorithms, and it really helps to feel good about completed tasks right from the beginning. They also have two parts to every task, with the second part adding up more difficulty on top of the first. In some challenges, the second part will have to work with the larger set of data, so you will quickly see if some solutions are not effective and require a second thought.
- Codewars — another great resource. You can find programming challenges for different levels of difficulty, as well as for different programming languages.
- Codility for programmers — it has both programming challenges and written lessons to help you with the theoretical basis. Unlike the previous resources that check you code performance very loosely, Codility does a great job checking your solutions in terms of time and space complexity, so I’d put it in the more advanced category.
- HackerRank — a popular resource that in addition to algorithmic challenges includes tutorials that help you learn programming languages and concepts.
So pick some that you like and start cracking! Solving algorithmic challenges can be difficult at first (especially if you haven’t tried them before), but it is a great way to grow as a developer, to land a better job, and, once you get a taste of it, it is also a lot of fun. Happy coding!