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.
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.
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!
Dashboard | HackerRank
Join over 7 million developers in solving code challenges on HackerRank, one of the best ways to prepare for…
LeetCode - The World's Leading Online Programming Learning Platform
At LeetCode, our mission is to help you improve yourself and land your dream job. We have a sizable repository of…