How To Solve Your Problems

Photo by on

Hello, World! I am Josh, a self-taught software engineer. I have been in the engineering world for a little over a year now. While I certainly have a TON to learn, I have picked up some skills along the way that I wanted to share! When I was first learning web development, I didn’t know where to begin. I was overwhelmed by all of the choices. Libraries, frameworks and languages, oh my!

I think the best tool in my arsenal is my approach to problem solving. While it is not a unique approach, it is tried and true. I hope by breaking down the problem solving approach, I can help other Jr. Developers gain the confidence needed to tackle coding challenges of increasing difficulty.

First, let us define what an Algorithm is. The textbook definition is, “a process or set of steps to accomplish a certain task”. For developers, algorithms are the foundation for being a successful problem solver. So without further ado, here are my 5 steps to solve any problem.

  1. Understand the Problem

Understanding the problem may seem like an obvious first step, or like it shouldn’t be a step at all. But it is quite often overlooked by young developers, eager to start smashing keys and cracking the code. However, taking a few minutes to really understand the problem will make you more efficient, lead to cleaner code and organize your approach. There are a few questions we can ask ourselves.

How would I explain the problem to a rubber duck? At this point, I may have lost all my credibility with you. Just hear me out for a second. Restating the problem out loud has a proven history of making sure you truly understand the problem. It helps you verbally lay out a roadmap.

What are the inputs to the problem? What are our outputs going to look like? Without understanding your inputs, it will be impossible to try and solve the problem. By defining your inputs, and comparing them to what your outputs should be, you have a clear starting point and a final destination.

Now let’s go one step further. Can our outputs be determined from our inputs? Meaning, do we need any external data or functionality to solve our problem? Are we working with a pure function? Answering these questions can help you decide where to start writing code.

This last tip applies to more than just this scenario and is good to start implementing right away. How should you label important data? Important variables and pieces of data should be labeled in an obvious way so that an outside user understands what the data does or represents. You want your code to read as close to English as possible. This keeps code clean and maintainable. Can you imagine trying to debug someone’s code if all their variables were single characters? Save yourself and others a headache and define your data and variables in a readable way!

2. Test Cases

I am only going to touch briefly on this subject. Someday in the future I will write another article about TDD and its benefits. For now, we will discuss a bare bones scenario. When given a coding challenge, we already discussed how important it is to understand your inputs. Now we will look at testing your outputs with some test cases.

Start simple, if your code is to find the sum of a + b, we can insert integers such as (1 +1). Once we have our brute solution down, we can expand our test cases to catch edge cases.

Explore more complex inputs, what if a and b were not just integers. What if they were something like nsquared, or the result of another function? This may be stretching this example to the extreme, but it sheds light on building solid test cases. By figuring out our worst case scenario and planning for it, we can ensure our solution will stand the test of time and handle edge cases.

Sometimes it makes sense to also test your algorithm with empty inputs or incorrect inputs (what if we made b an object in the above scenario?). Again, by testing edge cases and setting up expected outputs, we will have a clearer picture of the code that will be needed to solve the problem

Photo by on

3. Break It Down

This step is probably the most important of them all. If you’re anything like me, when you first started coding you went in guns blazing. I would barely be done reading the problem before my fingers hit the keys. While this approach may work for some, I have found that jumping into code before breaking down the problem can lead to cumbersome, messy code.

Before you start hacking away, take a minute to re-read the problem. Write down your steps as pseudo code. Break down the problem into the smallest chunks possible. When you get to more advanced coding problems, this key will save you time while giving you a clearer picture of how to solve the problem.

Let’s say you have a problem where you have to take a string and print out a string in reverse order. Breaking down the problem into pseudo code would look something like this.

  1. Break input into an array with .split(‘’);
  2. Reverse array with .reverse();
  3. Turn array back into a string with .join(‘’);
  4. Return the string

By breaking down the problem into small chunks, we have given ourselves a clear roadmap on how to solve a problem. Our next point is closely related to this step.

4. Solve/Simplify

Solve and Simplify. We can do this step while we are breaking our problem down. For this step, we want to take the complicated aspects of our problem and simplify them down into easy to solve chunks. In a perfect world, you would see the problem and know exactly how to solve it. For more difficult tasks, it is a much better approach to break the problem down into its simplest form.

If you are stuck with a complicated edge case, ignore it at first. Try to solve the problem as simply as possible. After you have come up with a brute force solution, go back and refactor to try and catch your edge case. This leads us to our final step.

5. Refactor

At this point we should have a working solution. It may not be pretty, but it gets the job done. Now we can go back into our solution and clean things up a bit. This is where we can analyze the space/time complexity and try to make our solution as efficient as possible. Personally, I think this is one area that separates a good programmer from a great one. The desire to make your code as clean and efficient as possible will pay off in the long run.

Photo by on

So there we have it, 5 steps to solve any problem that comes your way. By breaking down our problem into manageable chunks and understanding what our input and output should be, we give ourselves a fighting chance to solve even the most difficult coding problems. I hope this article helps you out! Please take a second to give me some claps and a follow if you’re up to it. I will continue posting articles on a weekly basis. Cheers and have a great day!

--

--

--

Full-stack web developer who is passionate about learning and creating!

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Object Relationships

Migrating emails from G Suite Legacy to a normal Gmail account with Got Your Back and Google Cloud…

How to Use Your Google Analytics Nano Project to Boost Your Visibility

Super easy way to setup 🚢 Docker container for mysql database

What Neil Gaiman Can Teach You About Learning to Code

API example project using Laravel PHP — Github Example Project Link

What is Wayland?

We’re going live in 3 days, we need performance testing!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Josh O'leary

Josh O'leary

Full-stack web developer who is passionate about learning and creating!

More from Medium

First taste of software development work — fighting COVID at MOH Singapore

5 Questions You Should Be Asking Before Starting Functions

5 bad coding habits you need to put an end to right now

Principles of Software Development & Introduction to JavaScript