Solving Your First Competitive Coding Problem

This is story is the successor of my previous story on Competitive coding. Today will solve a simple coding problem on hackerrank. I will try to explain as much as I Can.

Devansh Gupta
5 min readSep 24, 2019

The First Step

To start using hackerrank, You have to create a hackerrank account. This is the link to create the Account.

Hackerrank Signup Page

After creating your account, you are ready to go.

Hackerrank has a friendly Dashboard to get things started.

Next Step

This is the time to select the problem to solve. As I mentioned in the previous story, Beginners should start with a simple problem. Today we will solve this Problem. I found it easy to get started.

Simple Array Sum Problem on Hackerrank

Let’s examine every component of this problem statement.

First 2 lines tell us what is the problem, we are going to solve. In above Problem “We are given an array and we have to print the sum of it’s element”.

For now we will ignore the Function Description part.

Input Format

This tell us the format in which the platform will provide us the input. In Our case there will be 2 Lines

  1. First Line will tell the no. of elements present in Array, say n.
  2. Second line will contain the array, say ar. The array will be in provided as space separate values. This means “1 2 3 4 10 11” should be evaluate as [1,2,3,4,10,11].

Constraints

Almost every competitive coding problem contains some input constraints. But, why do we need them. Remember in last story, I told you guys that these problems have unknown input both in Magnitude and Size. But we need some constraints on input to get an idea of problem complexity. In simple words, It help us to choose the right data structure, algorithm and data type. For example if you are using C to solve the problem. You have to choose whether to choose int, long or long long to hold some value.

In above problem n (which is size of array) has at most 1000 elements and every element of array ar (ar[i]) ≤1000. and every array element is positive (>0). This helps us a lot while coding, we will see it later.

Output Format

This tell us how we are going to display the output. In above problem, it asked us to just print the sum of array as a single integer.

Sample Input

This tell us, how is input will look like.

Sample Output

This tell us, how the output will look like against above input. For example, first line of input is size of array (6), next line contains array containing 6 elements. “1 2 3 4 10 11”. and the output will be “31”.

Screenshot of Simple Array Problem (2)

Explanation

This is the most helpful part of any problem statement, because it tells us how the output is calculated against the given input. In above example it say why output is 3,1 because “1+2+3+4+10+11 = 31”. Some times explanation will tell us more than the actual problem statement.

So Let’s write the code to solve our first problem, I will right the code for C and Python to get gist of solution in these languages. I will try to make each and every step clear.

C Code to solve above problem

Look at the C code of solving this problem, We didn’t include conio.h , because all the online compilers are gcc compiler, and they don’t support conio.h, it only works in Turbo C world. Also we don’t need clrscr() or getch() like function. In above code we used an int array instead of long array because we know that elements of array are ≤ 1000 , and int can hold that value easily. Most of you will ask , why we used dynamic array instead of a static ones. The answer is because we don’t know the size of array, the array can contain 9 or 999 elements.

Look at the way, I read input , I simply read the integer supplied. I didn’t do anything more. Some people are used to normal coding in which they supply a helping text like “Enter the no. of element” or something, which is not required in competitive coding because all the input is supplied by an online server. So you just required to grab the input and nothing more. Just follow the instructions mentioned in Input Format section.

After calculating the sum of array, we need to output the answer, and again we just need to print a single integer and nothing more like “Sum of array elements is blah! blah!”. Just follow the instruction written in Output Format section.

It is good practice to free the dynamic memory you acquired, because some problems have memory constraints also.

Python Code to solve the problem

Here is the code in python to solve the problem. As we know that input() returns a string read from standard console and array length is integer. So we used int() to convert string to integer. And as the whole array is supplied in line 2 of input , we read that input as a single string using input() and then split that string to an array of string elements, then we used a list argument to convert string element into int element.

Next we just sum up, whole array, and print the sum of array.

Some specific things about Hackerrank

Hackerrank provides an appealing Online code editor, which is very useful and you can use it for coding your solution. It also provides support of >30 Programming languages. You can choose your favourite one from the Top-Right Drop down menu.

This code editor provides 2 button Run Code and Submit Code. Run code test your solution against the input they have given in sample problem and Submit Code tests your code against hidden test cases.

Conclusion Competitive coding is an interesting sport. You can learn a lot while playing it. And it can also help to secure a decent job. So start it as soon as possible. Here is my story about “Why and How to start Competitive Coding ?”.

Follow me on Instagram to stay updated about new stories.

--

--