How to Solve any Programming Problem
As humans, we solve problems all the time and as developers, it is no different. Problem-solving-focused courses are not very popular or common and a lot of developers tend to focus on learning tools, languages, and frameworks rather than learning how to think like a problem solver, a programmer.
What is Problem Solving?
Problem-solving can mean different things for different people or situations so it is good to clarify what this article means when “problem-solving” is mentioned.
Due to convenience, when you take your broken car to the shop they may decide to fix what is broken, replace the broken part, or offer the option for you to buy a new car altogether. Even though all these options seem like a “solution”, only the first one is actually dealing with the problem. Everything else is avoiding having to deal with the problem.
As developers, there may be times you have to decide to avoid the problem completely and replace it with something new instead of dealing with the problem, but this article is focused on techniques to solve the problem and not replace or avoid it.
You solve a problem when given a set of constraints and having to follow some rules you come up with a solution that meets all the constraints and does not break the rules. As programmers, we write a program, a set of instructions that solves the problem.
To Code is different than To Solve Problem
Anyone who takes the time to learn how to code will be able to program eventually. Learning to code is just like learning a new language. It is the ability to create instructions for the computer to execute by using a language that interpreted or compiled a computer is able to understand.
Solving problem is a different skillset and as humans, we are naturally good at it. I mean, we built the world around us by solving problem after problem. What developers often struggle with is connecting these 2 skillset. Solving programming problems makes you better at solving real-world problems and if you are good at solving real-world problems, programming may come naturally to you.
Why not many courses on this?
If you look around, there are thousands of courses that teach you how to use tools, how to connect one tool to another, and build things, but don't make obvious how they approach the problem. Of course, understanding a language, a tool or framework is important but it does not make you a problem solver.
Just because you understand the tools in the toolbox does not mean you can fix a car, but knowledge of the toolset and a problem-solving strategy and mindset may allow you to fix a car eventually.
What coding tutorials are good for is expose you to as many problems and solutions as possible as a way to teach you how to program. Developers tend to freak out when they don't find the video or the blog post with the answer and that's why they post on forums or social media websites like Reddit and StackOverflow.
Before the popularity of the internet, programmers had to actually find a solution for the problem they were dealing with. Back then, having a problem-solving strategy and mindset was essential and nowadays developers tend to neglect this skillset because they can just google the solutions.
This convenience of the internet made learning how to program easier and something you don't even have to go to school for but also flooded the industry with a lot of developers that can code but don't have the problem-solving strategy mindset.
Common Problem Solving Mistakes
What new developers often do is jump straight into coding things. They often have to adapt and change the code as they learn, which is not a bad skill to have, but you can go round and round and never reach a solution. This brute force approach through trial and error may work and be nice for prototype and exploration but even in those cases, it is best to have a plan.
It’s nobody’s fault actually. If you stop to think about how people are learning to program nowadays, you realize that is basically by clicking on a video and get the exact solution you need. Naively, you may try to do the same thing when trying to solve problems on your own.
What is often not obvious is that in order to put together a tutorial video, an instructor often goes through a process backed up by years of experience and exposure to many problems and solutions which makes it easy for him/her to walk you through the solution without having to explain how they reached that solution.
No, the tutorials are not bad things and you should not stop watching tutorials or google for solutions. Even pro developers do the same thing. What I am trying to make you understand is that you should put some time and effort into learning the process and plan your solution before you dive deep into coding.
So how do you solve problems?
This is a skill tested briefly during coding sessions of job interviews. When you go for a tech interview you may get tested for knowledge on certain tools (usually low or entry-level jobs) or problem-solving skills (normally mid to high-level job positions) and this is because professional developers and engineers often have to face problems that are not too specific to a tool which takes them out of their comfort zone.
Some problem solutions may even require you to create your own tool or system. That’s why big frameworks, tools, and systems are born in high-level jobs and tech companies. Facebook introduced React and GraphQl, Google developers created Angular and Dart, Microsoft gave us Typescript, etc. This shows that when you actually engage in solving a problem it may lead to new tools or systems that can be shared with others.
Every problem solver has a method, a step-by-step guide to reach the solution, and that’s what you need to become familiar with. Actually, your brain already knows this but it reaches solutions so fast that you often dont think about how it got there. To solve problems you need to slow down and gain control over the anxiety to reach the solution quickly.
You solve problems with a good problem approach strategy by analyzing, studying, and planning solutions, sometimes multiple ones. It may take time and requires patience. So let’s break it down.
Understand the problem
A problem may appear to be complex if you don't understand all the details. In job interviews, the first step is to understand what they are asking before you code a solution. When freelancing, you must understand clearly what the client is asking before you propose a solution. As a developer, you must understand the requirements and acceptance criteria of the task before you implement the solution.
If you can explain a problem in simple words to someone else and they get it, then you understand it. It is good to write down your understanding, sketch, make diagrams, etc. Often times other developers call me to their desk to help them solve something and just by trying to explain it to me they figure out the solution.
Your brain often gets stuck in some particular details that sometimes prevents you from seeing the bigger picture and by trying to explain it, sketch it, and breaking it down, you force your brain to grab more details that enrich your understanding of the problem.
The way you confirm your understanding of the problem may be by repeating it back to the person introducing you to the problem or by having confirmation on the details you collected.
Always Plan a solution first
Once you have the details and understand the problem it is time to plan a solution. This is the part where you analyze the constraints and the rules, strategize a step-by-step solution and come up with testing data or hypotheses to test against it.
You can do it by writing comments, high-level pseudocode, a diagram, flowchart, more detailed sketches, etc. The purpose of the planning phase is to see the bigger picture and identify all the subproblems, steps, and parts of the problem.
Your plan may change during the solution implementation or get replaced with something totally different as you go. The idea that spending time on a plan that may be scratched later may seem like a waste of time, but the simple step of planning for something gives you the idea of the whole thing and makes it much easier to adapt on the fly later on.
It is impossible to predict the whole outcome in one plan and that's why plans may change or get abandoned as soon as you work on implementing them. Without a plan, you are simply rolling the dice and hoping to get lucky. Lacking a plan makes it hard to delegate or welcome people into the solution as well.
Start with what you know
If planed correctly, you should be able to identify many parts of the problem and it is always best to solve for the things you are familiar with first. This approach can help you stay motivated about solving the problem and may spark new ideas on how you can address the rest of the problem.
Sometimes, a similar problem was already resolved by someone else. A problem can even share patterns with some others that are much simpler. Here is where good research and experience can help you easily identify details or parts of the problem that resembles something that was solved previously by you or some other developer.
Once you identify the patterns, you already solved the problem. If changes are required, you will have a starting point to continue from.
Start with the most constraints first
If you have to figure out a place to go grab lunch with your coworkers, the best option is to start with the person with the most diet constraints and rules first then continue from there. Once you solve for the most constraints you narrow down your options and make it solving for everything else much easier.
This is equivalent to buying a new house. You list all your constraints first; how much you can put down, how many bedrooms and bathrooms, area, budget, size, etc. Once you have all your details down you narrow your options making it much clearer to see what you can afford. If not satisfied, it will be equally obvious what you need to work on to change your options.
Divide the problem
Some problems cannot be solved in one solution. If you have to solve sudoku or a chess game, trying to come up with a plan for all the moves needed to win the game makes finding the solution a complex task. You should always divide your problems into chunks, solve all the chunks then solve the whole thing.
Simplify the problem
Simplify is different than dividing a problem. Simplifying means changing the constraints or rules. In the tech world, it is often referred to as finding the MVP solution. You pretty much reduce the scope of the problem to make the problem simpler. Once you have a solution for the simpler version of the problem, it provides us with enough knowledge, experience, and research time to solve the bigger version of it later.
There will be times which trying things and observing the results is the best way to approach solving the problem but it is not to be confused with typing some code and hoping that you will get the right result. Experimentation is a controlled process meant to explore the unknown or collect information.
Maybe there is a library that can help you solve the problem but you are not familiar with. Maybe you are expecting a result but got something totally different. Experimentation is a particularly great way to approach bug fixes. In general, it is a good approach to explore a problem to try to understand it more or to test different options to compare which can be a better solution.
Backup and state the problem
Sometimes, even when you collect all details, plan, and chunk the problem you cannot reach a solution. If you feel you are getting frustrated it is best to back up, calm down and re-state the problem. You should never spin your wheels on something that is affecting your mood.
Once you get frustrated you tend to complicate things, fail or pick a complex solution for the problem. Walk away for the day or for hours, take a deep breath and explain the problem back to you out loud. Sketch what is going on to check your understanding of the problem. Solving problems should be a logical thing and not an emotional one.
Solving problem is about exposing yourself to as many situations as possible and practice these strategies over and over. With time, it becomes second nature and a natural way you approach any problems in general. Big or small, always start with a plan, use other strategies mentioned here till you are confident and ready to code the solution.
Becoming a problem-solver takes time and practice. When it comes to programming, understanding fundamentals, data structures, algorithms, design patterns, and how things work is a more important knowledge to have as they can be applied in any programming language and environment.
With a problem-solving mindset, anything is possible. Write down these steps and try to follow them in your next problem-solving adventure until you memorize them. There are many websites that allow you to test your problem-solving skills for FREE that you should try. For more concrete examples and details, read the book below.
This article was inspired by the book “Think Like a Programmer” by Anton Spraul.