Solving Coding Challenges

200
0

Solving coding challenges is an everyday life experience for developers and programmers. It’s the bread they eat and the tea they drink; maybe that’s why you see them addicted to coffee and caffeine. Coding challenges aren’t peculiar to a level of developers, it’s peculiar to all developers regardless if they are juniors or seniors, though the level of difficulty may differ. For almost every application or website you are building, there are coding challenges waiting for you. For almost every technical interview you attempt, you can’t do away with encountering coding challenges. So, you will learn a lot from this article on how to attempt coding challenges in your everyday life or interviews.

There is no manual for solving a particular coding challenge because all challenges are peculiar in their way. Sometimes, the problems may seem similar, but their approaches have to be different even if the results may arrive at the same thing. Approaching coding challenges differ from person to person, but some processes can’t be ignored.

// From the array below, return a new set of array with only one set of fruit each.
    
const ourArray = ['mango', 'grapes', 'pear', 'strawberry', 'oranges', 'pear', 'apples', 'mango']

Understanding The Problem:

Identifying the problem is the hardest part of solving any coding challenge, and also it’s the first solution to the problem. When you are able to identify the problem, the process to the solution can be put together bit by bit.
Let’s try to solve the above coding challenge to put our problem-solving technique into practice.

For this coding challenge, understanding what we are told to do is the guide to putting our solutions in steps. And from the instruction in the comments, we are required to do two (2) things:
1. Return a new set of arrays: With this, we already know our result is expected to be an array, not an integer or a string. So this is the first step to our solution.
2. Remove duplicates: We are expected to return only one set of fruits each, with no repetition as in the original array.

With these 2 steps, we have been able to identify our challenges, then we can proceed to finding a solution to them.

Write Your Solutions Out:

Jot down if you have a chance to. It has a way of helping you memorize the challenge. When you write down the challenge, it’s easier to break down the solutions in steps and employ approaches to solving each step.

Break the problem down into smaller chunks because this helps you to solve the challenge one step at a time. It helps you write the solution down in smaller chunks as well, and then you can easily merge all of it into a bigger code block. Some challenges are easy to solve on paper, but interpreting the solution in code can take hours or even days

For the above coding challenge, after going back and forth on how to solve it, we arrive at one. Let’s try it out.

Using the array reduce method:

const ourArray = ['mango', 'grapes', 'pear', 'strawberry', 'oranges', 'pear', 'apples', 'mango']

  function removeDuplicates(arr) {
        const newWrray = ourArray.reduce( (acc, curr) =>{
            if (!acc.includes(curr))
                acc.push(curr);
            return acc;
        }, []);
        return newWrray;
    }
    console.log(removeDuplicates(ourArray))

The result will be:

Devise Alternate Solutions:

There is always an alternative approach to every problem, so always have one. Having an alternative solution somehow means you understand the problem quite well, even though arriving at this conclusion can take many trials. For the above challenge, let’s see another alternative solution to choose from.

Using the Set method:

const ourArray = ['mango', 'grapes', 'pear', 'strawberry', 'oranges', 'pear', 'apples', 'mango']

 let newArray = [...new Set(ourArray)];

console.log(newArray);

It outputs the same result.

Attempt The Challenge Before Seeking Help:

Always avoid copying and pasting code solutions. They don’t help you as much. They are only a temporary solution to a permanent problem. When you encounter the same challenge in the future, do you go back to copying the code and pasting it? It does you a lot of good to attempt the challenge a couple of times before resorting to copying and pasting. When you make an effort to solve the problem, you understand some steps in the solution. It also makes you understand the problem better, and as I said earlier, understanding the challenge is the first step to a solution.

For every coding challenge solved, there is an experience gained. And if you had to solve most of the challenge on your own without copying and pasting code, there is an invaluable wealth of knowledge gained because it makes you familiar with the challenge and wherever you see similar challenges, all it takes is having to remember how you solved the earlier one.

Refactor code:

Understand that you don’t just walk straight into a solution. A lot of times you have to rewrite the purported solution several times to arrive at a satisfactory solution. And every time you rewrite the solution, you refactor the code.

I hope this article is clear and helpful enough and I hope you had a great read. Please LIKE and refer someone to read as well. Also, don’t forget to follow me.

Have a great weekend.

oluwatrillions
WRITTEN BY

oluwatrillions

FullStack JavaScript Developer | Writer

Newsletter

Loading