Competitive Programming and Where to Begin?
Before we dive into “Everything you need to know about Competitive Programming”, who is this blog for?
If you are new to coding with no prior experience whatsoever. You have some coding knowledge, but want to get into Competitive coding.
Maybe you are already into Competitive Coding but need some help or this blog just popped up in your feed. Everyone is welcome here and this was made just for you! :)
What is Competitive Programming?
Competitive Programming (CP), yes, let’s call it CP. So, CP could be considered as a Sport. A Mind Sport. In CP you are given a problem, it forces you to come up with an approach that should give the desired output based on any input they pass. You need to think of an approach termed as an algorithm, and then write the code for it and submit that code. CP requires you to use your Logical thinking, Analytical thinking, Pattern Recognition skills, and most importantly, Programming skills, to solve that problem. It’s like that tricky Maths Puzzle your friend gave you to solve during your school days. 🧩
Why should I learn CP?
Okay, now that we know what Competitive Programming is, you should also know why there is so much hype around it. Why do so many students learning to code, hop into CP? Here are some reasons:
- It helps to improve your Problem Solving and Logical Skills.
- It is a great way to practice your Coding Skills.
- That satisfaction you get knowing that your code passed all test cases is beyond everything.
- Being good at CP increases your chances of getting recruited by huge MNCs. Companies like Microsoft, Google, Amazon, etc. require freshers to know a decent amount of CP because they wish to build code from the ground up, where they might require root code to build their products.
How do I get started?
1. Learning your First Programming Language.
It’s equally important to learn to code, as much it is to know how to think about a problem. For someone who has no prior experience in coding, I would recommend C++. Languages like C++, Java, Python are quite popular among coders doing CP.
I will not recommend Python for CP. Even though Python is very easy to learn, it is not that efficient when it comes to CP. Many CP Contests don’t even allow Python.
Java is good, a lot of Competitive Coders use Java. The Collections Framework in Java is quite useful. But C++ will be the best choice for CP. C++ is the fastest among these three. The STL library (it’s an inbuilt library in C++) has a lot of Predefined Functions and Data Structures that make doing CP a lot easier.
You don’t need to learn 100% of the language before starting to solve CP. I would suggest that you learn the basics of the language, then start with learning & solving CP Problems (which we will get into in a bit) and then learn the OOPs (Object Oriented Programming) part of the language.
2. Solving CP Problems
After knowing the basics of the language, it’s time to get your hands dirty with solving problems. The FUN part! 😃
Your first question might be — “Okay but where are these questions, and how do I solve them?”. There are multiple online CP platforms like Codechef, HackerRank, Codeforces, LeetCode, SPOJ, etc. These platforms are where coders go, to solve CP Problems. To start learning, I would recommend InterviewBit. InterviewBit is a platform where you can Learn as well as Practice along.
On InterviewBit, Go to Practice tab -> Programming Track. Here, you can see a vertical flowing list of topics. They include various Levels. Starting from Level 1, which includes the concept of Time Complexity. Each Level consists of Tutorials that you will require to learn that topic and various buckets of problems. Each Bucket consists of multiple problems using the same concept. Solving at least one question from each bucket would ensure that you understood the topic. Although I will recommend solving all the questions if you have time.
Each problem would contain The Problem Statement which explains the problem and what you have to solve. Then there will be the Input section, which explains in what format the input will be provided by the judge.
The first line contains an integer T, the total number of test cases. Then T lines follow, each line contains three integers A, B, C denoting the length of 3 sides of triangle.
Here you need to accept their input in your code. Let’s take integer T. You loop T times, and in each iteration, you take 3 numbers A, B, and C.
Then the problem would contain the output statement, explaining how you should output your final answer.
For each test case, display ‘YES’ if the triangle is valid, and ‘NO’, if it is not, in a new line.
So, from the output and input statement itself, you might have guessed that you would need to check if the given triangle is valid or not.
Some questions also have a Constraints section, which specifies how big the value of input will be.
1 ≤ T ≤ 1000
1 ≤ A, B, C ≤ 10000
According to the constraints, there could be up to 1000 test cases, with each case having values for the sides of the triangle up to 10⁴.
And there will be sample Input and sample Output sections, which usually give few examples of input and output for a better understanding of the problem.
I will recommend learning and solving in the proper flow given on InterviewBit.
3. Participating in Contests
After solving some problems, you will start to develop an intuition of it. Along with learning on InterviewBit, now you can also start to participate in contests. The best way to start is to participate in the Codechef Long Challenges. These are contests which last for 10 days, and contain about 6–8 problems. 2 problems are easy, 2–3 are medium and others are hard. Participate in Long Challenges and try to at least solve the first 2–3 problems. Since you will have 10 days, you will have enough time to think and code them. After the contest ends, the Editorials for the contest will be posted. Refer to the Editorial if you were not able to solve any problem from those easy 2–3 problems.
Daily learning, practice, and Participating in Long Challenges will improve your problem-solving skills/ability. Now, you would notice that you would not be able to solve the remaining questions from the contest. The reason for that is DSA. 🤔
Data Structures and Algorithms
Till now you would have been solving questions using simple Arrays and Arithmetic. But this is not sufficient while solving all kinds of questions. Some problems require more than what Arrays can offer. Here comes Data Structures and Algorithms.
What are Data Structures? Just like you use Array to store data, other types of Structures store data efficiently inside memory blocks in a way to make it easy to store and retrieve data. In fact, arrays themselves are one of the Data Structures. Others include Linked Lists, Stack, Queue, Maps, Graphs, Pairs, Hash Maps, etc.
Then there are Algorithms. Algorithms are a predefined set of steps or procedures to be performed for a specific task. For example, you want to sort an Array in ascending order, then there are many Sorting Algorithms like Bubble Sort, Insertion Sort, Selection Sort, Merge Sort, etc. Depending on your requirements you may implement and use one of these algorithms to solve your problem. Knowing DSA really helps your CP Skills. Some problems have a time constraint on them like 1 second. In that case, a naive brute force approach you have thought might not run within the required time limit. Hence, you will need to think about using such optimized Data Structures and Algorithms in your problem to make the computation faster.
Where to learn DSA from? Here are some resources that would help.
- Data Structures from mycodeschool
- Data Structures from GeeksforGeeks
- Algorithms by Abdul Bari
- Algorithms from GeeksforGeeks
- Rachit Jain DSA Guide
- Handbook for CP (Very useful)
- Learning Dynamic Programming by Aditya Verma (For Learning)
- Educational DP Contest — AtCoder (For Practice)
- Number Theory by CodeNCode (For those nasty Math CP Problems)
- Errichto — YouTube (One of the BEST CP Coder)
- Back To Back SWE — Youtube (Great Intuition behind difficult DSA Concepts)
By the way, if you want to get an In-depth Guide about DSA, Check out this amazing blog by one of my seniors, Shreyansh Chheda.
This contains everything you need to starting from DSA to Projects and finally to Coding Interviews. 🚀
Practice! Practice! Practice!
After learning DSA, now it all comes to practicing daily and participating in CP Contests. At this point, you could start participating in Codechef Lunchtime and Cookoff Challenges. These are short challenges of 3 hours. Participating in these along with learning on InterviewBit would help you to get faster in solving problems.
At first, you might be able to solve only 1 or 2 problems. But as you practice, things will improve. After the contest ends, take your time to up-solve the questions you were not able to solve during the contest. Up-solving is one of the most important practices in CP. As you get better, you could also participate on other platforms like Codeforces. Codeforces conducts a lot of challenges, each of 2 hours. Virtual Contests on Codeforces are very useful.
My Experience with Coding and CP
Now you might have a clear roadmap on how to start with Competitive Programming. Before I share some of my experience with Coding and CP, I will like to make it clear that this is my personal experience and will not apply to everyone. I had started with CP in my 8th grade (2015) but back then I had no idea that this is called CP. I used to do it for fun. At that time HackerRank was quite famous, so I used to do a lot of HackerRank. But not knowing DSA, I was only able to solve Easy Problems using naive, Brute force techniques. Later I moved on to Development related programming which includes stuff like Web Development, Android Development, etc.
Later, when I got admission to VJTI, through our seniors and internet I came to know about DSA, and how that helps in CP. Following all the above-mentioned Resources, I started learning DSA along with practicing CP Problems and participating in Codechef Long Challenges. From being able to solve only 1 question in my first contest to be consistently able to solve about 5 questions, DSA really helped. After that, I also started practicing on Codeforces short contest. Making a transition from Long Challenges to Short Contests, was hard. Also participated in various other Contests like Google Code Jam, Google Kick Start, etc. Recently, I came to know about InterviewBit and how it so easy for beginners to start with CP on InterviewBit. For someone starting now, InterviewBit would be a great first choice.
Mistakes to avoid in CP
There is no progress without making mistakes. But here are few mistakes beginners fall into,
- Avoid focusing too much on your Ratings on such platforms. Solve problems, don’t run behind ratings in the beginning.
- LEARNING before solving and competing is most important. You should always LEARN a concept first before trying to spend time, solving problems on it.
- Stuck on a problem? Feels tempting to peep a little into the solution or editorial? If you have some idea about that topic, then avoid looking into the editorial without giving your 100% attempt. If you are short on time, learn the problem type, and then solve a problem of the same type.
- Never hesitate to take help from your friends or seniors.
- Up-solving might feel boring, but that’s the only way to improve.
- After solving a problem, take some time to analyze your solution. Things like finding its Time and Space Complexity helps in the long run.
- It’s easy to feel demotivated if you are not able to solve a question, don’t give up so easily. Improvement in CP takes time, at the end it all feels rewarding.
Closing on this blog, I will like to say that,
CP is not Proficiency in DSA. CP is a hobby. Proficiency in DSA is a skill if not a necessity.
I hope you loved reading this blog and found it helpful. Enjoy Competitive Programming. 😄