How to approach any coding problem? by@madhavbahl

January 17th 2019 16,321 reads

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 ✨

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”.

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 —

- College Placements
- Writing good quality code
- Efficient code (Time and space complexity)
- Making optimum use of resources
- Logical reasoning
- Exposure (there is a huge community of students who do CP)
- Opportunities (Tons of CP Contests out there)
- 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,

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 😁

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.

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

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.)

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

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?

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**

- Does this code run for every possible input (including the edge cases)
- Is there any other way to solve the problem?
- Is the code efficient? Can it be more efficient?
- Is the code readable?
- If someone else showed you this code, would you be able to understand it?
- Can the performance be improved?
- 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 😃

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

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:

Email: madhavbahl10@gmail.com

Web: http://madhavbahl.tech/

Github: https://github.com/MadhavBahlMD

LinkedIn: https://www.linkedin.com/in/madhavbahl/