Having strong problem solving skills is an essential part of being a developer. When faced with an abstract or complex problem, knowing how to approach, think through, and tackle a problem will set you up for success. But for many new programmers, improving their problem solving skills is one of the hardest parts about learning to program.
When faced with a new problem, having a strategy that you use each time for tackling new or difficult problems will dramatically increases your ability to solve problems. Below is my well tested strategy that I use in real life and interviews when faced with any programming problem.
Step 1: Read and understand the problem
This may sound simple, but many people do not read and understand the problem they have to solve. They just skip to the code as soon as they’ve heard the problem. This is a big mistake. Reading and understanding what you’re trying to solve is the first step to solving any problem. When you read a new problem, take note of key points. These include things like parameters the function is taking it, the data structures of the parameters, the name of the function if it has one, and what it's returning. If the problem mentions any edge cases, such as is the return value must be sorted alphabetically, that is also good to note.
Also, if you don’t understand a problem completely in the beginning, that’s ok. If you’re in an interview, feel free to ask clarifying questions to solidify your understanding. Programming interviews are not the place to “fake it until you make it”. If you are just practicing a problem at home, move on to Step 2: Look at the tests. Tests can help you immensely when it comes to understanding the problem you’re trying to solve.
Step 2: Look at the tests
Not all problems you run into will have tests, but if they do, you should take a look at them. In programming, tests are extremely important. Tests let you know what the expected output of a function should be given a specific input. Tests basically tell you if your code is working correctly.
When looking at the tests, take a look at what type of parameters the functions are taking in and what is being returned. This will help you in the next section when you plan out your code.
Step 3: Pseudocode
When you look at a problem and its tests, you may have an idea of exactly how to write the code. If you know what to do, that's great. For more complex problems, you may need to take a moment to break down the code. When you run across a harder problem, I alway recommend pseudocoding out the problem. When you pseudocode, you write out the steps you want to take in your code in plain English so you have a roadmap to follow when writing your code. You'll see me do some pseudocoding in the vast majority of the solution videos for the Coding Bootcamp Prep course. I do this because it gives me a roadmap to follow when I'm writing the code and helps me remember everything that I have to do. There is a great section on pseudocoding in a blog post I wrote called How To Handle Difficult Bootcamp Interview Questions.
When pseudocoding, start at the very beginning of a problem. Sometimes, I like to start with the name of the function and what parameters it's taking in. If you get stuck on how to approach a problem, take a look back at the first test case. A lot of the time, tests start with the most basic functionality and then get more complex.
Step 4: Write the code and run the tests
Now that you've written your pseudocode, use it to actually write your code. Once you have an implementation, run your tests. If all the tests return true, great. If some of them fail, don't freak out. This is actually a good thing. Failing tests just means that your code isn't working as expected, and you may need to reexamine your logic. We've all had this happen. Even the best programmers don't always get their tests to pass on the first try.
If your taking a course, one of the most important things to do when your tests don't pass is to try again. Failing tests or other code errors like syntax errors provide a great learning opportunity and will help you improve your problem solving and programming skills. The people that I see who struggle the most when learning to program don't try to debug their code; they just skip to the solution. This is a huge disservice because when you get a job as a developer, there is no answer key. You have to figure everything out yourself or have one of your teammates take time to help. So, practicing the problems solving process early will help you develop your problem solving skills.
Step 5: Debug if needed and change your code
When your tests don't pass, it's time to start debugging :). Debugging is an extremely important skill to have as a developer. The Coding Bootcamp Prep course contains an entire section that walks new programmers through debugging their code.
Once you think you know the part of the code that is giving you trouble and why it’s causing problems, make your changes, and rerun your tests. If your tests still don’t pass, continue to try to debug your code until you can get all your tests to pass.
This article was written as part of the Coding Bootcamp Prep course. The course is dedicated to providing people of all skill levels the knowledge they need to get admitted to the country's top coding bootcamps.