Coding challenges are absolutely crucial to your learning but can often be very difficult to solve. We've previously shared an article with the best websites and resources to practice with, and now we're focusing on some actionable overall advice.
Mastering algorithms and data structures can help you supercharge your coding challenge solutions and step ahead of your peers. That said, they take years of practice to master, so it’s easy to get stuck as a beginner. But, it is an essential skill every software developer should have, the importance being:
However, most developers don’t focus much on this. If you happen to be among those who do - well, that’s probably why you are reading this - then these tips will help you become better at tackling coding problems.
Again, don’t rush - you’re not trying to catch a bus.
A lot of people jump right into coding without a proper understanding of the problem, then get stuck, rather than taking time to first understand the problem. Understanding the problem is the most important aspect of solving any coding challenge - and really, the most important part of solving any problem in life, as you can’t solve what you don’t understand.
Once you understand the coding problem, try solving it on a piece of paper first. Don’t worry about the code, syntax, or efficiency just yet. Simply focus on this - can you solve it? If ‘yes’, then congratulations, you just increased your problem-solving skills by 10! If ‘no’, then continue staring at the paper until you’re able to solve it.
While some might disagree, solving on paper saves you a lot of time and energy because it defines the structure of your code, and if you’re able to do it then, you will just have the code to worry about.
When solving a problem, break things into smaller steps. This helps in building simple code, analyzing what makes it work, and discovering other solutions. Depending on the magnitude of the problem, you can create a solution to pass just one test case before considering other cases or the edge cases.
Let’s look at an example - the “Repeated String” challenge;
The challenge is to find and print the number of occurrences of the letter “a” in the first n letters of an infinite string.
{% code-block language="js" %}
def repeatedString(s, n)
end
{% code-block-end %}
s is the string to be repeated.
n is the number of characters to consider.
If we have a string ‘aba’ and n is 6 then the substring to consider will be ‘abaaba’. The letter “a” appears 4 times.
This is quite easy to solve on paper but how can the computer solve it? Let’s break the solution into smaller steps:
There are likely better ways of solving this, but it’s an example of how to break down solutions.
It can be tempting to look up solutions to a challenge but this is bad practice. It’s bad practice as it’s easy to be constrained to thinking in a certain way after looking at the solution, so you have to resist - resist it like you do procrastination.
In all seriousness, looking up the solution should be your last resort. If you must look it up, get a solution that has everything explained in detail, this will ensure you understand the logic behind the solution and you’re not just memorizing the code. Then, once you understand the logic, come up with a different solution. Your solution might not be as effective as the one you saw but this process will improve your problem-solving skills.
No amount of hardware is going to compensate for an inefficient algorithm.
Examine your solution carefully; this is where you should pay attention to time and space complexity. The explanation for that is beyond the scope of this article but you can read more about it in this article.
When looking at your solution; can you replace that “sort” method? Can you remove the nested loop? LeetCode is a good place to check the time and space complexity of your code, I remember shaving off milliseconds from my code by replacing the sort method. This might not sound much, but a few milliseconds is equivalent to three years in the coding world -- that’s an exaggeration but I hope you get the point.
Practice makes perfect... The more coding challenges you do, the better you become at them. Start with the easy ones and move your way up. Then come back to the challenges you’ve completed and try to solve them differently or solve similar challenges.
Remember, everything takes time, don’t beat yourself up if you can’t solve a challenge!
I hope this helped you understand how you can improve at coding challenges…. happy coding!
Career advice, the latest coding trends and languages, and insights on how to land a remote job in tech, straight to your inbox.