Before you go, check out these stories!

Hackernoon logoHow to approach any coding problem? by@madhavbahl

How to approach any coding problem?

Author profile picture

@madhavbahlMadhav Bahl

“First, solve the problem. Then, write the code.” — John Johnson
Whenever any coding problem is given, by basic instincts most of the people follow some predefined steps which allows them to approach that problem effectively. Knowingly or unknowingly, even you might be following some of these steps when you are given any question unconsciously, but after reading this article you will try to relate to these amazing steps and you’ll be able to solve the problem more effectively. 
So in this article, I am going to discuss 5 steps that you can take while solving any coding question ✨

The world requires devs, why should I spend time on Competitive Programming?

Being a developer myself, I found that most of the developers out there (including many “very” skilled devs), are not that good when it comes to data structures, algorithms and problem solving in general. It’s a sad thing that these days many people are just cramming some basic steps to develop an application, or a simple software and calling themselves “coders”.

Relatable? (source:

With everyone going for development, learning in depth about data structures and algorithms have become a second choice for students these days even though it is an essential domain for each student,

Even though I am a developer and not too much into competitive programming, still I try to give more time to learn about data structures and algorithms and how to write efficient code, and I encourage and recommend that everyone should focus on this as well.

So here are some examples where data structures, algorithms, and competitive programming (in general) helps out students —

  1. College Placements
  2. Writing good quality code
  3. Efficient code (Time and space complexity)
  4. Making optimum use of resources
  5. Logical reasoning
  6. Exposure (there is a huge community of students who do CP)
  7. Opportunities (Tons of CP Contests out there)
  8. You will have a lot of fun in the process
“Code teaches you how to face really big problems
— Jack Dorsey

Now moving on to the topic,

The 5 Steps I follow whenever I am given any coding question

A few days ago while doing a coding question, I found that I have a pattern in whatever problem I solve. I did some more problems and kept writing down what all was there in my mind before and after I solve any problem. To my surprise there was a common pattern, unknowingly I was doing these 5 things and now that I know about these 5 steps, I am able to solve problems more efficiently.

Again, as I said in the intro, it is highly probable that you are also using these steps unconsciously, but when you will find out you’ll start applying these consciously and then notice the difference 😁

1. Understand and Analyse the Problem

Read the problem, start thinking about it and if possible write the things that are given and the things that you need to find out on a piece of paper.

Ask yourself,

— Are you able to understand the question fully?
 — Would you be able to explain the question to a layman?
 — What and how many inputs are required?
 — What would be the output for those inputs
 — Can you separate out some modules or parts from the problem?
 — Do you have enough information? If not, try understanding the question again.

See that you don’t get stuck xD (source:

2. Go through the sample inputs and examples thoroughly

Going through some sample inputs and coming up with more examples sure helps you a lot to understand the problem well, and moreover, it gives you a clear way to how many cases your code should handle, and what all can be the possible output or output range.

Here’s what I would suggest,

— Take very simple examples and find the output
 — Take more complex and bigger inputs to see what will be the output, how many use cases do we want

Then, “Time to handle the edge cases”

— Try out the problem with no input, what should be the output now
 — Try out the problem with invalid input, what should be the output now

Too Many Indeed! (source: Google Images)

3. Break down the problem

This step is really very helpful when the problem at hand is very big. In such cases, I recommend the following,

— Try to make a flow chart or a UML for the problem in hand
 — Divide the problem into different modules or sub-problems
 — Try to make independent functions for each sub-problem
 — Connect those sub-problems by calling them in the required order, or as necessary (Probably one function would be calling another)
 — Try to use classes and objects while handling questions which try to implement some real-world problem (like management systems, etc.)

The flow chart which programmers really need 😂 (Source: Google Images)

4. Start solving/coding

Now that you’ve analyzed the problem it’s time to actually write the code. If you can’t solve the problem fully at once, try writing code for a simpler sub part, as you solve the problem, gradually you keep figuring out the problem in more detail, and you start getting new ideas.

Just keep in mind 3 things, and you’ll surely figure out the path 😁

— The point where you started
 — Where are you right now?
 — What is your destination?

Also, when you are giving some interview, do not waste time in figuring out the whole solution and then tell your interviewer, keep simplifying the problem and keep telling your interviewer about how you are approaching the problem

— Tell the interviewer how you are trying to start
 — Tell what method is there in your mind right now
 — Find out the most difficult part you are facing in that problem
 — Ignore that “most difficult” part for some time and start solving a simpler sub-part, that’ll buy you more time to think about the former part
 — Once done with the simple sub-parts, try incorporating a similar approach for the difficult part as well
 — You might come up with a better solution while doing the problem, tell that to your interviewer

5. Look Back and Learn More

Now, this is the most important part! Once you are done, look back whether the code can be improved, is there any other way to solve the given problem?

Source: Google Images
Don’t just give up when you are done, give up when you feel content with the possible solution(s) and you’ve explored the problem completely!

Here are some questions you should ask yourself once you are done writing the code

  1. Does this code run for every possible input (including the edge cases)
  2. Is there any other way to solve the problem?
  3. Is the code efficient? Can it be more efficient?
  4. Is the code readable?
  5. If someone else showed you this code, would you be able to understand it?
  6. Can the performance be improved?
  7. Can some other algorithm be used which provides better results?

Apart from these questions, do check out other people’s solution to the same problem also, check out how they solved it, you learn a lot while seeing other’s codes.


Here are the 5 points we discussed in this article

👉 Analyze and understand the problem
👉 Have a look at sample inputs and use cases
👉 Break down the problem into smaller subparts, or functions, or modules
👉 Start solving/coding
👉 Once you are done, look back whether the code can be improved, is there any other way to solve the given problem,

That’s it, I hoped this article would add value to your problem-solving skills 😃

Want to improve your skills?

Interested in improving your skills in data structures, algorithms, and competitive programming? I’ve started an initiative called “Daily Codes” where we solve one question each day together, do check out this repository:

Do give it a ⭐ if you found it useful

Daily Codes

Hope you found the article helpful 😁

Feel free to reach out to me anytime if you want to discuss something :D

I would be more than happy if you send your feedbacks, suggestions or ask queries. Moreover, I love to make new friends and we can be friends, just drop me a mail.

Thanks a lot for reading till end. You can contact me in case if you need any assistance:


The Noonification banner

Subscribe to get your daily round-up of top tech stories!