Steps for Solving Any Algorithm

I’ve been solving algorithms on websites like hacker rank and leetcode since graduating from Flatiron. Through research and self-trial I’ve found a few steps that help me solve and optimize my code. Even if you’re unable to optimize your solution these steps will act as a guide to help you get to that point with practice.

1. Listen to/ Read carefully

Every detail in an algorithm will be needed to solve it optimally. Sure you can probably get away with not using one of the details at some point. However, this negligence will most likely affect the efficiency of your final solution.

2. Ask clarifying questions

Whether you're solving the algorithm alone, or in a technical interview asking questions out loud will help dissolve any concerns you have. It’ll also help you discover an edge case you’ll have to work through.

3. Brute Force Solution

Before attempting to code the optimal solution take a quick moment to figure out how’d you’d solve it with brute force. This step isn’t always necessary if you do know the optimal solution immediately. If the most optimal solution doesn't come to mind it’s always better to have something to work with than nothing at all. This could mean pseudo coding out the steps you’d take or coding the brute solution. Whichever you choose to do is a great step that will aid in optimizing your code in the future.

4a. State Run Time/ Time

If you chose to pseudo code and/or code out the brute force solution it is important to understand how efficient your solution is before optimizing. Big O notation is the most common metric for measuring time complexity. It describes the execution time of a task concerning the number of steps that are required to complete it. This topic is a recurring question during technical interviews and is an important tool for software engineers to measure their code's efficiency.

4b. Optimize

If you chose to skip step three, you can start to code out the optimized solution and move on step five. In this step, you can start to walk through your solution and

5. State Run Time/ Time

As mentioned earlier, Big O notation is quite important, and understanding the runtime of our solution can help in a variety of ways.

6. Test Code

Use simple test cases at first, then continue to test your code with more complex test cases as well as edge cases. It’s important to test your code with more than just one example as it will limit your ability to find bugs in your code.

Conclusion

These steps aren’t a magical set of rules that will instantly make you an algorithm whiz. It will however help you make incremental improvement and I have found that following these steps has improved my code’s efficiency and my own understanding of algorithms. I’ve included the sites I use to practice algorithms. With that being said practice is the best way to improve so… happy coding!

Software Engineering Student at Flatiron School! Recently graduated as a Programming & Software Development Major at LaGuardia Community College.

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