How to solve a coding problem

A new skillset to learn.

·

4 min read

There is a chance that you may have heard that there is no right way how to solve a problem. Every coder has a method, a step-by-step guide on how to find the solution, and this is personal, this is creative freedom.

Not everyone thinks the same, and this is good news. Allowing us to have a different approach for a complex task, and look for the most commons solutions for the repetitive ones.

We can go through some steps to the problem-solving method:

  • Understand the problem
  • Plan a Solution
  • Break into small pieces
  • Translate to code.

In the first three steps, you don't need an editor.

Let’s start it.

Understand the Problem

https://images.unsplash.com/photo-1504639725590-34d0984388bd?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1074&q=80

Every time a code-related problem appears, it seems to rush the anxiety to open your favorite editor as soon as possible. First, you need to understand the problem.

Read the problem out loud, talk to you about what the problem is asking. Clarify everything you didn't understand. Ask questions!

There is nothing wrong to don’t fully understanding the problem at first.

If someone tells you that this is easy, this person is lying.

Now, it’s time to start sketching, simulate inputs, draw arrows for the output. Take notes on what kind of instructions are happening to obtain that result. Let's plan a solution!

Plan a Solution

https://images.unsplash.com/photo-1581291519195-ef11498d1cf2?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1470&q=80

The magic word here is pseudocode.

Pseudocode is the bridge between the program and the algorithm.

You can read it better here: https://www.unf.edu/~broggio/cop2221/2221pseu.htm

Do you remember your sketch before?

Keep taking notes, but now give a name for your arrows.

Is it conditional? Is this repetitive? Should I loop through it?

There is no perfect plan.

  1. Identify all the steps.
  2. Identify the parts of the problem.
  3. Guide yourself through your logic.

There is a high chance that your solution may change. And trust me, it is not a waste of time.

trial and error is part of the process. Trust the process!

If you consider yourself a perfectionist, you may think, why? Why would I even try if there is a high chance it won't work?

Because the simple step of planning gives you the idea of the whole thing, what works, what don't.

Here you are going to use everything you have studied. And it is an opportunity to learn on-demand.

You don't know the tools or building blocks to this specific task. Search, study for this purpose, and return to your pseudocode.

There is no right or wrong here, do in a way you understand and help you.

Break the Problem into small pieces

https://images.unsplash.com/photo-1533234427049-9e9bb093186d?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1470&q=80

There are many coding challenges that can be solved with one instruction. There will be times when you’re going to need the evaluation of one instruction, to go to the next step. Since most of the kinds of stuff that we do in our daily lives we do automatically, we might think that coding is the same thing.

For example, to drink water:

  1. Go to the kitchen
  2. Open the container that holds the drinking glass
  3. Find a drinking glass
  4. Pick up the drinking glass
  5. Walk to the source of drinking water
  6. Put the drinking glass in a position to be filled.
  7. Fill the glass with water
  8. When the glass is full ⇒ Drink.

https://images.unsplash.com/photo-1642952469120-eed4b65104be?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1470&q=80

Do we think in all of these steps? No.

Each of these steps is linked, you need to complete one instruction to go to the next one. All these processes are known as Algorithms.

Get your sketch again, and divide, break your problem, everything that seems complex, break it. Divide into chunks, solve all those chunks, then you can solve the whole.

Keep it short, and simple.

Translate to Code

https://images.unsplash.com/photo-1567646303972-f7de3a9c0a05?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=687&q=80

The next step is to solve the problem. Using your notes and your pseudocode as your guide, write out your actual code.

Now you can open your IDE and write your code. There will be times when trying and observing the results is the best way, called Experimentation. However, this works best when you have domain about what you’re doing.

Don’t expect for typing and magically working.

What if you can’t solve the entire problem?

Practice!

https://images.unsplash.com/photo-1536158525388-65ad2110afc8?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1470&q=80

If you got stuck, there is no problem looking for other solutions and how other people solve them. But return to the same problem a while later, and try to do it again.

As I said in the beginning, solving problems is a whole new skill set.

Only by practicing and exposing yourself, over and over, you’ll improve it.

All solutions start with a plan.