Getting started with Competitive Programming
While going for an interview of your dream company, the biggest fear that most of the candidates have is the coding questions because every interview consists of some kind of coding rounds where you will be having one problem and you need to solve that problem in a limited amount of time. At that time, if you haven't done competitive programming before, then you will start regretting. So, don't worry, in this blog, we will see how to get started with Competitive Programming to crack the coding interviews. Below is the timeline of this blog:
- What is Competitive Programming?
- Benefits of Competitive Programming
- How to get started?
- Common terms used in Competitive Programming
- How to approach a problem in Competitive Programming?
- Participate in online challenges
- Learn by doing
So, let's get started.
What is Competitive Programming?
Here is the definition of Competitive Programming from Wikipedia:
Competitive programming is a mind sport usually held over the Internet or a local network, involving participants trying to program according to provided specifications. Contestants are referred to as sport programmers.
In simple words, it is a type of contest where a number of programmers take part to solve some programming questions in a limited amount of time. Either the contests can be online on various coding platforms or it can be held offline in some schools or colleges or organizations.
Benefits of Competitive Programming
So, we have seen what is competitive programming and before starting it, we should know some of the benefits of Competitive Programming:
- Problem-solving: In competitive programming, you will be given some real-word problems and you need to solve them in the best possible way in limited time. So, this will improve your daily life problem-solving capability.
- Coding interviews: Competitive Programming will help you in solving the questions of a coding interview in more than one possible way. Generally, an interviewer asks for more than one solution for a particular problem.
- Get in your dream company: Various companies like Google, Facebook, Amazon, etc conduct online coding competitions and they directly hire candidates based on their performance in the competition.
- Profile building: When you solve questions on a particular platform or in some competitions, then you are building your profile on that particular platform. Many companies tend to hire candidates, based on their profile on some online coding platform.
- Teamwork: Most of the coding competitions happen in groups. So, you learn how to perform and manage time during some competition.
So, these are some of the benefits of Competitive Programming. Now, let's see how to get started with Competitive Programming.
How to get started with Competitive Programming?
Follow the below steps to get started with Competitive Programming:
Choose one language
Before starting, you should be familiar with at least one programming language like c++, java or python. If you know more than one language, then you should choose the one in which you are more comfortable. Also, try to choose some object-oriented language. Most of the people use C++ for Competitive Programming.
Choose an online platform
There are many online coding platforms where you can practice and take part in some online challenges. So, choose any one and start solving problems. Some of the online coding platforms are Hackerrank, CodeChef, HackerEarth, etc.
Learn about time and space complexity
In competitive coding, time and space complexity is the most important factor because for every question there is some time and space limit and within that limit, your code should run. In most of the cases, the time limit is 1 sec and the space limit is 256 MB. (Learn about Time and Space complexity from here)
Learn Data Structures and Desing & Analysis of Algorithms
Data Structures and Design & Analysis of Algorithms is very important for solving a question in the best possible way. Some of the Data Structures that you need during Competitive Programming are:
Concepts that you need to know:
- Time and Space Complexity
- Searching and sorting
- Bit Manipulation
- Greedy approach
- Dynamic programming
- Number theory
- Game theory
Practice more and more
Start practising problems on online coding websites and also participate in coding contests. Also, try to solve questions of the past coding contests and other problems available on the website. You can also practice selected questions of every topic from here.
Common terms used in Competitive Programming
While attempting some question on any website, you will encounter some terms that will be present in almost every problem. Some of these terms are:
Every question will have one paragraph that will show the problem description. Here each and every detail of the problem will be given i.e. what to do? Read the problem statement carefully. Sometimes, to make the problem more realistic, the description may be large. So, read it carefully.
Following is an example of the Birthday Cake Candles problem from HackerRank:
The input/output format of the questions in competitive programming is different from the normal input/output format that we use. For example, if we want to take input in an array, then normally we do the following:
Enter the size of an array: 5 Enter data: 1 2 3 4 5
But in competitive coding, the above will look like this:
5 1 2 3 4 5
So, have a close look at the input format of the questions in competitive coding and take inputs and give outputs accordingly.
For some questions, you will be given the information about the input sizes i.e. the range in which the input data can vary. This will help you in deciding the time complexity of your code because the time complexity is proportional to the input size. So, always work according to the constraints given. Following is an example of constraints:
1 <= n <= 10^5 1 <= arr[i] <= 10^7
Each question will have some sample input/output that will show the explanation of the problem with the help of one input and the corresponding output. So, you can take help from that explanation and write your code accordingly. For example, if you are finding the sum of elements of an array, then the sample input/output can be:
Sample Input: 5 1 2 3 4 5 Sample output: 15
In some questions, you will get the explanation of the sample input and sample output. This is just for understanding the purpose of the user.
Testcases are the inputs and output files on which your code will be validated. When you write code, then the validation of the code is done with the help of some test cases and if your code passes all the test cases then your code is right otherwise, you need some improvement in your code. So, test cases are basically input files along with output files that verify your code.
Some test cases are public and you can see them and write your code accordingly if there is some error. But some test cases are private i.e. those test cases are not visible to users but they are used for code verification.
Time and space limit
In some cases, you will be given time and space limit of your code. Generally, 1 sec time and 256MB space are given to run a particular code. If your code is using more than the allowed time, then you will get the Time Limit Exceed error or simply TLE error and in that case, you need to reduce the complexity of your code.
How to approach a problem in Competitive Programming?
There can be many ways of solving a problem in some competitive coding. You can follow the below steps to solve a problem:
- Understand problem statement: Always give time to read the problem statement carefully. Don't be in a hurry, when you are reading the problem because this may result in the wrong interpretation of the problem and as a result of this, you will write the wrong code. So, read the problem carefully.
- Note down the important points: To make a problem more realistic, many unrelevant information may be there in the problem statement. So, while reading the problem, make a note of important points that you are going to solve.
- Look for constraints: Before writing the code, you should look for the input/output constraints given in the question. This will help you in deciding the time complexity of your code and you need not change the code again and again because if the code is not according to the constraints, then you will get the TLE error.
- Input/output format: Always take input in the given format and never use your own format. If you are not following it, then you will get the wrong answer instead of having the right code.
- Write the code and test: After deciding the time complexity, write the code and first test your code for the sample input and if it passes then submit your code and test for other test cases.
Participate in Online Coding Contests
Always participate in Online Coding Contests that happens regularly on various websites. If you are new, then you will learn a lot of things like how to approach a problem and find the best solution. These online challenges maybe some hiring challenge for the company or some reward will be given to the users. You will learn to solve problems in limited time and in an intense situation and you can apply this is your life also. Some of Online Coding Contests are:
Learn by doing
Solve as many problems as possible and try to solve different problems. If you are stuck in some problem, then try harder and if you are not able to find the solution then see the hint or the solution of other users and try to code once on your own.
Slow and steady wins the race
Instead of solving many questions in one day and then not solving any question in the next few days, you should make a target that you will solve the minimum "n" questions per day. You can increase this limit but never decrease the limit. For example, make a habit of solving 4 questions daily and by the end of the year, you will solve 365 * 4 = 1460 questions :)
So, start Competitive Coding and you will land yourself in your dream company. Also, let us know if you face some problem while starting Competitive Programming or solving some question.
Happy Learning :)