Solving a Coding Problem - In Four Steps

**Plus bonus tips when all else fails**

For software devs new and old, the thing that remains constant is the need to solve problems. Whether it's to build on the fundamentals or to flex some serious code know-how for an efficient solution, there should be a general process in place to tackle all phases of a coding problem.

1. Understand the Problem

The first step before jumping into any coding problem is to understand what the problem is asking of you. Consider what the problem is asking you to take in (input) and what it's asking you to return (output). Does the question prompt you to ignore certain situations, or to assume that input values will be consistent? Create some example inputs and what it should output based on your understanding of the question. Once you have ensured that you've taken the necessary preparations, you can move into the second phase.

2. Create a plan

This is the part of the process that can be the most time consuming. Keep in mind, you're still not writing code yet in this phase. After understanding the problem, think about how many steps you need to take in order to arrive at the solution. If you need a visual guide, a piece of paper or a white board works great for this part of the exercise. Write down each step in easy to follow phrases -- this is known as writing pseudo code. Once you've finished, take some of your example inputs and run it through your pseudo code to check that it reaches the desired solution.

3. Execute your plan

When you feel confident in your planning, it's time to write some code! Translate your pseudo code into real code while trying not to optimize or second guess the steps that you wrote down in the planning phase. Confidently write down what you know and leave any steps that you don't know for later.

4. Review your code

Once you've finished writing your code, it's helpful to look back and review your code. Are there any redundant steps that could be optimized in few lines of code? Is there something that you did not consider and the output came out different? Take this time to look back and check your work! You may find that you can create a more efficient answer this way.

Bonus Tips - "What if I do all these steps and I still get stuck?"

If you went through all these steps and find yourself stuck at any point - here are a few tricks that I tend to use when I experience a rut in a coding problem:

  • Take a break; step away from the screen and take a moment to rest your eyes, drink water, and come back with fresh eyes.
  • Google! It's the best friend of all devs, new or old. If you're having trouble remembering a method or syntax, look it up on the mdn. You'll be able to solve the problem faster and you might learn a trick or two from the documentation!

Conclusion

I hope that these tips were useful and that you can use this method as a starting point for any coding problem! Have any input, or want to share your experience with this method? Feel free to let me know in the comments!