top of page
  • First Digital

The best way to approach any coding problem

Updated: Aug 10, 2022

Ever struggled for a few hours with a programming problem, only to find out after all that effort, that the problem could have been simple to solve? Ever felt disappointed in yourself for not thinking about certain solutions faster?

Being a developer myself, I can definitely answer yes to the above questions and I’m sure I’m not the only one. I found that most of the time when I approached a coding problem, I go through a few steps subconsciously. I feel this really helps me as a developer so I wanted to share it with you.

The steps I run through can be summed up into three simple questions…

What is the problem I am trying to solve?

One of the lessons I had to learn the hard way when I just started in the industry, was that you have to make absolutely sure that you understand the problem you are trying to solve in terms of what the problem is, why it is a problem in the first place and how to recreate the problem.

This is the most important step in solving any coding problem, as you can’t expect to solve a problem effectively, when you don’t really understand the problem properly.

How can I split the problem into smaller tasks?

Something I usually do, especially when I am faced with a bigger type of coding challenge/problem, is that I will try to break up the problem into smaller pieces that I can solve more easily.

I found that certain programming / coding problems can sometimes seem like a mountain in terms of complexity, but once it is broken down into smaller chunks, it does not seem so complex anymore, and can be solved easier and more efficiently.

How can I simplify my solution?

After I have created my solution, I often find that I can refactor my code and in some cases simplify my logic to increase the readably of my code and make my code more efficient.

This is something I only recently started doing, but in the long run this should be beneficial for future developers that need to work on code you wrote and for yourself as well, to easily understand the code you wrote a while back.

How do these steps relate to what is taught in the industry you might be asking yourself? Well if we have a look at the industry we can see that there are generally four steps which are recommended. These include…

· Understanding the problem.

· Devising a plan

· Carrying out that plan

· Looking back at what you did and learning from it.

These steps where first outlined in a book called “How to Solve It” by George Pólya. The book originally came out in 1945.

Looking at these steps you can see that generally the steps I recommend and have been following, follow the same idea in that the goal of both sets of steps are for you to understand the problem and learn from what you did.

Getting Practical…

What good are the steps if I cannot even show them being used in a practical example you might be asking. I use it practically every day, of which the following is an example…

The Problem…

“Write a program that asks the user for a number n and prints the sum of the numbers 1 to n.”

Let’s solve the above problem following the steps outlined…

What is the problem I am trying to solve?

· The problem I am trying to solve requires one input from the user in the form of an integer / number.

· The problem requires me to create a sum of all the numbers up until and including the one given as input and give the answer of the sum as output.

· The basic idea being… input: 3 => 1+2+3 => output: 6

How can I split the problem into smaller tasks?

Now that we know what the problem is and what is required from us in terms of inputs / outputs, we can start breaking down the problem into smaller tasks as follows…

· Get an input from the user in the form of a number/integer.

· Use a loop to loop through the different numbers and add them together.

· Print the result of the numbers added together in the loop as your output to the user.

Keeping the above steps in mind, I came up with the following solution:

How can I simplify my solution?

Looking at the above solution it is already quite simple as this problem didn’t require a complicated solution… but we can add some comments to make the code more readable.

Knowing that these steps have really helped me throughout my career as a developer, I am hopeful that you might have found some value through reading this post, even if it just inspired you to be more curious and code more!

Happy coding/developing my fellow developers!

By Rohan Krige

120 views0 comments


bottom of page