Hello, fellows!
Solving coding challenges will benefit you not just in the interview process, but also in your day-to-day life as a developer.
So, in this article, I'll walk you through several steps you can take to approach coding problems, and then I'll show you some practical CodeWars examples.
Letβs dive in. πββοΈ
1. Understand the problem π
We have a natural tendency to rush through activities, including problem solving. We begin attacking a problem without even knowing what we are trying to solve.
As a first step, try to read the problem carefully, ask yourself questions, simplify data, and only then begin to solve it.
Helpful questions: What do I know? What data is available? What should be the end result?
2. Make a strategy π
After I am sure that I have understood the problem, I usually formulate a plan, even writing down some step-by-step thoughts on how to address the problem.
Write your ideas in pseudocode.
3. Solve the problem π‘
The following step is to address the problem itself.
Of course, you may be tempted to seek easier solutions from others, but I encourage you to solve the problem as best you can. Don't get focused on the difficult parts; instead, concentrate on what can be done. Perhaps, in time, your mind will come up with some brilliant solutions to the difficult parts!
You can do it! π
4. Optimize the code π
After you've solved the problem and it's working successfully, you might spend some time improving and refactoring it to make it more effective.
Hereβs how I solved a CodeWars problem with JavaScript using these steps:
Moving Zeros To The End
The problem description:
Write an algorithm that takes an array and moves all of the zeros to the end, preserving the order of the other elements.
moveZeros([false,1,0,1,2,0,1,3,"a"]) // returns[false,1,1,2,1,3,"a",0,0]
1.What do I know and what are the inputs? What should be the end result?
I have an array as input and should return an array with all zeros moved to the end. The order of the other elements must also be maintained.
Here is a simplified example based on this problem:
moveZeros([2, 0, 5, 0, βaβ]) // returns [2, 5, βaβ, 0, 0]
2.Pseudocode
My first mechanical approach was to divide two arrays in order to keep order and separate from zeros:
Initialize array1 to empty array
Initialize an array for zeros to empty array
For each element in the input array
If the element equals 0
Push the 0 into array of zeros
Else
Push the element into array1
Return concatenated array1 with array of zeros
3.Solving the problem:
var moveZeros = function (arr) {
let array1 = [];
let arrayOfZeros = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i] === 0) {
arrayOfZeros.push(arr[i]);
} else {
array1.push(arr[i]);
}
}
return [...array1,...arrayOfZeros];
}
4.Optimizing the solution:
I was thinking about filtering elements as a way to improve the solution. Filtering elements with values other than zero and elements with values equal to zero.
filter
in JavaScript returns a new array containing filtered elements.
Finally, the optimized solution is as follows:
var moveZeros = function (arr) {
return [...arr.filter(value => value !== 0),...arr.filter(value => value === 0)];
}
We can also use concat
method in order to concatenate the filtered arrays:
var moveZeros = function (arr) {
return arr.filter(value => value !== 0).concat(arr.filter(value => value === 0));
}
Final thoughts
I hope this helps you overcome difficulties as a developer, whether in technical interviews or on a daily basis.
To begin, you may look for a better solution to the problem I discussed before. π
Last but not least, practicing problem solving with patience may lead to you becoming a better and faster problem solver. π¨π¨π¨
Keep up the good work until the next time! π