How To Solve Coding Problems Like A Pro
DEVELOPER PRODUCTIVITY
According to Indeed, an American worldwide employment website for job listings, problem-solving skills are the 4th most important skill required as a programmer.
Code is often written to solve a problem, when I mean problems I don't mean mistakes in your code, I mean real-world problems that require solutions. You should have at some point encountered problems in your code, how you solve these problems not only shows your level of experience as a developer but also speak volumes as to how much of the problem you comprehend.
In this article, I'll work you through 4 step framework on how to solve problems like a professional, we'll also solve a real problem to solidify our understanding.
Understand the problem
โIf I had an hour to solve a problem I'd spend 55 minutes thinking about the problem and five minutes thinking about solutions.โ -- Albert Einstein
Most of the hard problems we encounter are only hard because we don't understand them, understanding a problem simply means you know what is wrong and what it takes to correct it, one of the ways to know if you understand a problem is to try explaining it in simple terms to yourself.
If you can't explain the problem in simple terms then you simply don't understand it, to understand a problem try this;
- Read the question.
- Make your observations.
- Ask relevant questions.
We would understand this while solving the simple problem below.
Execute using JavaScript
If your age is greater than or equal to 18, log a string like this to the console: ''I am eligible to vote ''. Otherwise, log a string like this ''I am not eligible to vote because am ''2 years'' below voting age'' (the 2 is the 18 minus our current age)
We make the following observations
- If signifies a conditional statement.
- Greater than or equal to signifies a logical operator.
- We are required to log into the developer console.
- What does otherwise mean?; Ans -- since there is an if statement otherwise should mean an else statement.
- We are required to replace the ''2'' yrs with 18 minus our current age.
Divide and conquer
A problem well stated is a problem half solved. -- John Dewey
This is a popular strategy used by the army, you can't defeat a large enemy? divide them into small portions either by distraction or by taking calculated risks then attack them accordingly. The same applies in programming except that we are not distracting anybody or taking any risk, calculated or not.๐
Don't try to solve one big problem at a time, Always divide it into subproblems instead, you'll save yourself time, and stress and it makes the problem much easier to solve. To continue solving our problem we;
- We define our current age.
- We define the resultant age.
- We implement the conditional statements and log the strings to the console.
- We perform the alternative operation if our age is not up to 18
Research
"Research is what I'm doing when I don't know what I'm doing." -- Wernher von Braun
Whether you admit it or not, you don't know everything, and when you find yourself constantly hitting a wall, that is the point where you take a break and research. There is no problem you would come across that someone else hasn't encountered or solved.
It would interest you to know that some of the programmers you look up to are good researchers. The bulk of the job you'll do as a programmer is researching, this can't be overemphasized.
One thing you should take into consideration when researching is that you don't find the solution to a problem at once, especially when solving bigger problems, break the problem into sub-problems then make your research. What I mean is this, when we want to make research the problem we were given in this article, it would look like this;
- How to define a variable in javascript
- How to write conditional statements in javascript
- How to log strings to the developer console
Write Pseudocode
Pseudocode is the informal description of the actual code.
Pseudocode is code written for humans to understand, there are no rules when writing it, it's like writing in your customized programming language. Writing pseudocode aims to make the code easy to comprehend especially if someone else wants to go through your code.
Writing pseudocode might not be necessary when solving small problems like the one we have in this article but when solving bigger problems, writing pseudocode goes a long way to help simplify the problem.
The pseudocode for the problem we are solving would look like this.
Problem solution
We all have different approaches to solving a particular problem so my answer might differ from that of another.
Conclusion
Problem-solving is a broad topic and I only tried my best to capture what makes a better problem solver in this article, you would not become a professional by just solving one problem NO!. It takes consistent practice, solving a lot of problems would make you a better problem solver.
A good place to start practicing coding problems right away is leetcode, there are over 2300 questions for you to practice and you also get to participate in contests to challenge yourself and earn rewards. They didn't sponsor this article, by the way, I just love the platform.
So this is where I stop typing๐, I am a software engineer and a web3 enthusiast passionate about making productive developers, I plan on dedicating a couple of my next articles to educating people on the concept of cryptocurrencies and web3, would also love to connect with more techies on Twitter
If you found this article useful kindly drop a comment. WAGMI๐