Leetcode ‘Power of Three’ walkthrough
Studying data structures, algorithms, and practicing interview questions are essential parts of preparing to land your first job as a software engineer. Throughout my job search so far, I have been putting time into these endeavors but have definitely spent more of my time working on projects. I much prefer building sites and applications to doing interview questions, and it is essential to growing as a developer. At the same time, practice problems also help sharpen my skills and will make me better at working on my projects, just not as directly. I have resolved to spend a minimum of one hour per day working on leetcode problems, at least until I get my first developer job.
The problem I will be solving today is called power of 3(find it here if you want to give it a try before reading this). Essentially, I need to write a function that takes a number as an argument, then returns true if that number is a power of three, and false if it is not. For example, 27 would return true, because 3 to the 3rd power is 27. Conversely, 6 would return false, because while 6 may be a multiple of 3, there is no power that 3 can be taken to to equal 6.
Initially, I thought that the question was asking me to return true if the argument was any number to the third power, not any number that is a power of 3. Things like this are extremely important to pay attention to, as they can cause you to waste a ton of time. In this case, I spent about 30 minutes trying to solve this problem under my incorrect assumption, and got extremely frustrated. I wrote a working solution to what I thought was the question, and thought that I must just be a terrible programmer. Thankfully, I’m not a terrible programmer, just an occasionally hasty and presumptuous one. I’m sure any developer, whether they are in their first year of programming or their 10th, has made simple mistakes like this. It reminds me a lot of an instructor that I had at the coding bootcamp that I attended. He reminded us every day that it was essential to “read the README”, and most of us still managed to misread the instructions at least once.
Right off the bat, we can make sure to return false if the number we are checking is 0. It is important to consider edge cases like this and try to address them early on in the problem solving process, so that they do not break your code after you have already put tons of effort in.
I approached this problem by thinking about the nature of power(exponents, not power in general). What is something that all powers of three have in common? They are all divisible by 3. So, one quick way to weed out numbers that are not powers of three is by checking if they are evenly divisible by 3. The modulo operator is extremely useful in situations like these. You can divide basically anything by 3, so regular division would not be a productive way to go.
Another important thing to know is that any number to the power of 0 is equal to 1. So, if you divide 3 by 3, you get 1. In other words, 3 divided by 3 equals 3 to the power of 0. This means that any number that is a power of 3 will eventually equal 1 if you call modulo 3 on it enough times. Conversely, any number that is not a power of three will not.
Given all of this information, I decided to solve this problem using a while loop. There is a screenshot of my solution below, as well as a summary of all the steps.
- Deal with the edge case of the number being 0. If you take out the logic to deal with this edge case and the input is 0, you will get stuck in an infinite loop. Why? Because 0 modulo 3 does equal 0, but my solution will just continue to divide 0 by 3(which still equals 0) over and over again.
- I chose to proceed from here with a while loop. Since the goal is to continue dividing the number by 3 until it equals one, the condition I set is while n modulo 3 equals 0. This will stop the loop as soon as the condition is not true, eliminating all falsey answers. Inside of the curly brackets, I simply divided n by 3.
- Finally, a simple way to return true if the input is a power of 3 is to say ‘return n === 1’ . Since this will only be true if the number is a power of three, it will return false in all other situations.
I hope that you enjoyed this guide, and that beyond that, it helps with future problems that you try to solve!