Learn to Code by Competitive Programming

How do I Learn to Code? This is probably the most nagging question at the back of your mind once you have decided that you want to learn how to program. Like learning anything else, there is no standard process for learning to code. Of course, there are guidelines, there are courses, there are ideologies, and there are set traditions, but there is no one correct way.

You can begin with is Competitive Programming. Getting started with it is quite easy and if you devote a sufficient amount of time and effort, you can develop a very strong grasp of programming logic in a relatively short amount of time.

Here are some steps to help you get started and be good at it.

  • Get comfortable writing code in one of these languages C, C++, or Java. Why only C, C++ or Java? Because these are the standard languages allowed in any programming competition.
  • If you are already good at C, you should learn C++. It is the most popular language among competitive programmers because of its speed and an excellent library in the form of STL (Standard Template Library).
  • Pick an online judge. Recommended ones are Topcoder and Codeforces. These sites have high-quality problems and allow you to see another’s code post contest completion. These also categorize problems based on the topic. Some other popular judges include SPOJ, CodeChef (powered by SPOJ), and HackerEarth.
  • Start with simple problems that typically require transforming English to code and does not require any knowledge of algorithms. Solving Div 2 250 (Division 2, 250 points) in Topcoder or Div 2 Problem A in Codeforces is a good start.
  • In the early stages of programming, people tend to write long pieces of code, which is actually not required. Try to keep the codes short and simple.
  • Practice these problems until you become comfortable that you can submit it for 240 odd points on any day.

Start implementing basic (or standard) algorithms. I suggest you read them from Topcoder tutorials or Introduction to algorithms.

  1. Graph algorithms: Breadth first search (BFS), Depth first search(DFS), Strongly connected components (SCC), Dijkstra, Floyd-Warshall, Minimum spanning tree(MST), and Topological sort
  2. Dynamic programming: Standard dynamic programming problems such as Rod Cutting, Knapsack, Matrix chain multiplication, etc
  3. Number theory: Modular arithmetic, Fermat’s theorem, Chinese remainder theorem(CRT), Euclidean method for GCD, Logarithmic Exponentiation, Sieve of Eratosthenes, and Euler's totient function
  4. Greedy: Standard problems such as Activity selection
  5. Search techniques: Binary search, Ternary search, and Meet in the middle
  6. Data structures (Basic): Stacks, Queues, Trees, and Heaps
  7. Data structures (Advanced): Trie, Segment trees, Fenwick tree or Binary indexed tree(BIT), and Disjoint data structures
  8. Strings: Knuth Morris Pratt (KMP), Z algorithm, Suffix arrays/Suffix trees (a bit advanced algorithms)
  9. Computational geometry: Graham-Scan for convex hull, and Line sweep
  10. Game theory: Basic principles of Nim game, Grundy numbers, and Sprague-Grundy theorem

The list is not complete, but these are the ones that you encounter very often in the contests. There are other algorithms, but those are required very rarely in the contests.

How to monetize your programming skills

You can find description and implementation of standard algorithms here.

  • Once you have enough knowledge of popular algorithms, you can start solving the medium level problems. That is p 2 all problems in Topcoder and Codeforces. It is advisable not to go for p 1 500 at this point.
  • Learning to code is all about practicing. Participate regularly in the programming contests. Solve the ones that you cannot solve in the contest, after the contest. Apart from Topcoder and Codeforces you can also look at HackerEarth Challenges or Codechef contests.
  • Read the codes of highly rated programmers. Compare your solution with them. You can see that it is simple and shorter than your solution. Analyze how they have approached and improve your implementation skills.
  • Read the editorials after the contest. You can learn how to solve the problems that you were not able to solve in the contest and learn alternative ways to solve the problems which you could solve.
  • Always practice the problems that you could solve in the contest. If you are able to solve p 2 250 and 500 in the contest but not p 2 1000, then practice as many p 2 1000 problems as as you can.
  • Do not spend too much time if you are not getting the solution or are stuck somewhere.
  • After you feel that you have spent enough time, look at the editorials. Understand the algorithm and code it. Do not look at the real solution before you have attempted to write the code on your own.
  • Programming is a very practical and hands-on skill. You have to continuously do it to be good at it. It's not enough to solve the problem theoretically. You have to code it and get the solution accepted. Knowing which algorithm/logic to use and implementing it are two different things. It takes both to be good at programming.
  • The programming learning phase is going to take a lot of time and the key is practicing regularly. It will take some time before you can attempt p 1 500 and other tough problems. Do not give up on reading the editorials and implementing them even if it takes many hours/days. Remember that everything requires practice to master it.

It takes a considerable amount of time before you get good at it. You have to keep yourself motivated throughout. Forming a team and practicing is a good choice. Not giving up is the key here.

HackerEarth conducts monthly online programming contests. Register here to get started and receive further updates.

About the Author

Kaushik MV
Kaushik has been to ACM ICPC World Finals 2013 after winning Kharagpur regionals and has been actively programming in various competitive coding challenges for the last 3 years. You can find him on topcoder and codeforces.

Author Post

Thank you. Your subscription has been updated.

18
198
78