DEV Community

Cover image for The Best Way To Learn Algorithms and Data Structures

The Best Way To Learn Algorithms and Data Structures

salarc123 profile image SalarC123 ・5 min read

Leetcode? πŸ’€

When you hear the phrase, "algorithms and data structures," your first thought is likely Leetcode, but what if I told you that there was a more enjoyable and beneficial alternative to grinding Leetcode problems. I'm not claiming that Leetcode is useless because I know that it has helped millions of people pass interviews, but I just don't think that it's the most fun way to prepare for an interview. By doing Leetcode problems, your goal is to learn algorithms and develop better problem solving skills, so you shouldn't limit yourself to solely Leetcode when there are so many other options that fulfill the same goal. Breaking out of this narrow perspective can help you become more open minded and better at real world problem solving.

Sorry for the lengthy build up, but my solution is... competitive programming.

What Is Competitive Programming? πŸ’ͺ

Leetcode is useful but it's not that fun, so why not try competitive programming which is both. Competitive programming is the use of algorithms and data structures to efficiently solve coding problems. It provides you the opportunity to learn and even APPLY algorithms. If you get into competitive programming, you will be motivated to improve because you know that you can win competitions and get rewards, especially considering how many beginner competitions there are. You can develop an algorithmic sense of thinking that will carry over to interview prep, but even if you're not looking for a job, competitive programming is very helpful for developing problem solving skills in general.

Competitive programming does require a lot of time and effort to master, but it's all worth it because of the skills you acquire and the fun that you have while competing and learning. If you're really passionate about computer science, then you might not need as much motivation to learn algorithms and data structures, but you can get a leg up by starting to code competitively. According to Confucius, β€œChoose a job you love, and you will never have to work a day in your life,” so if you really think that competitive programming is for you, then you'll truly have fun while getting experience and developing real world skills at the same time.

The standard coding interview nowadays consists of a technical interview where you need to apply an algorithm to solve a challenging problem but with only a whiteboard and your mind. Thus, learning when to use algorithms, how to apply them to random problems, and how to articulate how they work is extremely important to learn, and competitive programming is the best way to achieve these goals, so let me tell you how to get started.

How To Get Started πŸš€

I propose that you learn C++ for competitive programming even if you already know Python or Java (which are both used pretty frequently in CP) because it's much faster and allows you to directly allocate memory, so you don't need to worry about the typical time and storage constraints of each problem when you’re starting out. For example, this problem inputs a number, n, that could be 1018 which would be very slow for a language like Python to work with. C++ is also the most popular language for competitive programming, so naturally, there should be more resources available to you. For instance, you can start with the very well written and free Competitive Programmer's Handbook as well as Darren Yao's book (specifically for the high school United States Computing Olympiad). And if you've attempted to learn C++ before but gotten stuck because there were too many concepts to learn, don't worry. When using C++ for competitive programming, you can ignore most of the standard best practices like using header files or object oriented programming because you'll usually be working in one file with a few functions.

Once you've learned the syntax of C++, you can start working on problems, preferably with TopCoder and CodeForces or you can try old USACO Contests (the USACO competitions are only for high schoolers, but the problems are still really good. These are the difficulty levels in increasing order: bronze, silver, gold, platinum). Just remember that consistency is key, so you should try to work for at least one hour a day on competitive programming. Some problems might just take you that long, and if they do, don't look at solutions unless you've tried solutions for an hour. If you have no idea where to start, you can look at a solution, but make sure to note down somewhere how the problem was approached and what you can learn in order to approach it the same way. It's important that you practice everyday. Keep doing this until you feel comfortable enough to try out some beginner competitions.

Problem Solving Strategies πŸ’₯

What I typically do to solve each problem is start by thinking of a solution and then testing it with paper and pencil. I have a notebook next to my computer so that I can write down notes for improvement and new tips that I learn as well as to document strategies for each problem type (rectangle geometry problems, for example, can be simplified by creating a Rect class with four variables as coordinates representing the bottom left and top right of a rectangle and an area method. This prevents messy code and lets you focus on the solution rather than debugging or creating tons of variables). When mapping out the solution on paper, also ensure that your strategy works for edge cases so that you can avoid implementing convoluted if statements. If you can't solve a problem (and even if you do), you should try to watch live solves on Youtube to see how the best competitive programmers approach the same problems.

Helpful Resources πŸ”₯


Complete Resource List


Youtubers / Playlists / Videos

Carrara - really good live solves with C++, Python, and Java

Errichto - I haven't watched many of his videos, but he makes solution videos and gives general competitive programming tips

William Lin - he has some introductory videos which you can use as a roadmap

Competitive programming tips other than "practice" - I thought this was pretty insightful because if you ever ask for help on Reddit for what you can do to improve, the only response you'll get is to practice more

Conclusion ✨

To wrap things up, you should, at a minimum, make sure that you're having fun. The career benefits are amazing, but they shouldn't be your main focus if you're really passionate about competitive programming. Don't worry if you're too old either because it's never too late to start competitive programming. So, just give it a try. If you don't have any motivation right now, push through until you can start competing in beginner contests because from there, you won't lose motivation again. Good luck!

If you have any other resources, comment them below!

Discussion (3)

Editor guide
salarc123 profile image
SalarC123 Author

These resources are really nice, but do you have any that apply specifically to competitive programming?

rohithv07 profile image
Rohith V • Edited

These are very handy for DSA and the basic concepts.

Second Thread :

ecnerwala :