A Beginner's Guide to Competitive Coding

Want to start competitive coding? But not exactly sure where to start and how to progress?
In this article, I will share some information and tips to help you get started with competitive coding. I have been into competitive coding for around 5 years now, have participated and ranked in many competitive coding contents like TCS Codevita, ACM ICPC, etc. The things I am going to share are mostly from my experience, which also includes some valuable tips which I have gained from other professional coders.


Why to engage in competitive coding?

Competitive Coding is basically a mind sport where one needs to quickly implement solutions for some math puzzles or some task problems. You get a problem statement, you read that and then logically think of a way to solve the problem and then implement that logic by writing some lines of code. This helps you to develop your problem-solving skills and also increases your thinking capability. If you are into Competitive Coding it will surely help you in your career whether you are a Software Developer, Data Scientist or, a Network Security Engineer. Most companies hiring freshers from campus placements have a round where they test the student's coding skills, also for top product-based companies one has to go through an initial round of competitive coding.


Data-structure and Algorithms, why they are important?

Well, if you want to excel in Competitive Coding then a proper understanding of Data-structure and Algorithms are must. Time constraint is an important factor while solving coding problems. By using a proper data-structure and implementing the best algorithm you can bring down the time complexity value of your solution(if you are not aware about time complexity, you can read about it here). Lets see an example of this.
Suppose we have a problem statement like this:
We are given an array and a target number. We need to find two numbers in the array that sum up to the target number provided.

Example:
Array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 15] , target-number: 18
Output: [3,15]

Solution 1:
def getTwoValue(arr,targetSum):
    length = len(arr)
    for i in range(0,length-1):
        for j in range(i+1,length):
            if(arr[i]+arr[j]==targetSum):
                return [arr[i], arr[j]]
##driver main
if __name__ == '__main__':
    arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 15]
    targetSum = 18
    result = getTwoValue(arr, targetSum)
    print(result)

Here we can see in the getTwoValue() function one for-loop is running inside another for-loop. This will result in a time-complexity of O(n^2).

Solution 2:
from collections import defaultdict 
def getTwoValue(array, targetSum):
    length = len(array)
    tempDict = defaultdict(lambda: False)
    for i in range(0,length):
	tempDict[array[i]]=True
    for i in range(0,length):
	remain = targetSum - array[i]
	if(tempDict[remain]==True and remain != array[i]):
		if(array[i]<remain):
		  return [array[i],remain]
		else:
		  return [remain,array[i]]
     return []
##driver main if __name__ == '__main__': arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 15] targetSum = 18 result = getTwoValue(arr, targetSum) print(result)

Here we can see in the getTwoValue() function we are using a hash-map. the variable tempDict is the hash-map being used here. Here, we can see there are two for-loops but they are independent of each other, i.e not nested inside one another, so as a result time-complexity here is O(n).

So, here we saw how using a proper Data Structure and implementing a different algorithm we reduced the time complexity of the problem from O(n^2) to O(n). So before just diving into competitive coding gain some knowledge on Data-Structures and Algorithm, it will surely help.

Which programming language should be used?

Generally, if your concepts are clear, language doesn't matter for competitive coding. Most of the competitive coding platforms allows you to write code in any programming language you want. So, language is not a bottleneck.
Still many developers prefer languages which has a simple syntax and concepts like OOP can be implemented easily. Based on survey results from top coding platforms the top 3 languages used for competitive coding were C++, Java and Python.

Platforms where one can practice competitive coding:

I will mention top 5 platforms which are mostly used my competitive coders:
The information shared here in this article, I feel is enough to get you started with competitive coding. If you still haven't started it, do ASAP. It is fun and you will surely enjoy it. Stay safe and happy coding!!

Post a Comment

0 Comments