Steps In Solving Coding Challenges

A beginners guide to problem-solving

ยท

8 min read

Steps In  Solving Coding Challenges

Many people can code, but not many people know how to use these coding principles to solve real-life problems.

Many developers find it difficult to solve problems despite the fact that they have the tools to do so. Many times, they just run to check it out on the web. While this step can be good sometimes, most of the time we do this prematurely. Before going to consult external resources, we should have made sure that we had given that problem a try in a systematic order.

We have always heard that it is good to break down our problems into a number of little chunks. While this is true, there are also other ways to help us achieve the solution in a very short time.

The concept of Data Structures and Algorithms (DSA) still remains a very important concept in solving coding challenges. So, a good understanding of DSA will aid us in solving problems not just accurately but efficiently.

dsa.jpg

Here are some resources that can help you understand DSA:

  1. Data Structures and Algorithm in Java - Pepcoding

  2. Coderbyte

  3. Hacker Rank

  4. Leet Code and many more.

You can also search for comprehensive courses on Udemy, Cousera, edX.

Here is a list of visual tools to help with understanding DSA:

  1. Visualgo
  2. Computer Science, University of San Francisco

In this article, I will be taking us through the basic steps of solving most coding challenges with ๐Ÿš€๐Ÿš€๐Ÿš€.

Remove fear and face the problem facing you

fear.jfif

Many people are always overwhelmed when they see these questions, and then they are not able to attempt the questions at all. The truth is that you might not be able to answer all questions because it is your first time and you are probably not familiar with the platform. I remember when I participated in my first coding challenge at TIIDELab, I was very shocked and overwhelmed. For 2 hours, I could not do anything. I was first of all new to the platform being used Coderbyte and I was confused about how to start solving the problems, maybe because it was my first time. When I discovered this challenge, I tried to prepare a strategic way to make sure I aced these coding challenges, and that's what I'll be sharing with you in the next steps.

Read and understand the problem given

read.jpg

It is always tempting to rush through these questions because of the limited time we have to answer them. While this can be tempting, we must resist the urge to skim through the questions. We must first read and understand the question. Sometimes I read out the questions to help me understand better.

It is always important to read to understand that there may be some conditions to solving that problem that may not follow the conventional conditions.

For example, there might be a condition that assumes A to be the last number in a sequence, but conventionally we know that A is always the first letter.

Most coding platforms give examples. It is also very important that we study these examples thoroughly and make our observations to help us understand the problem properly.

Another thing we also need to take note of is the input format and output format. This is important so as to tell us the type of operation that can be performed on that particular data type. This is another thing I do to help me out with these steps. To help me with this step, I use the MDN docs, which contain the properties and methods that can be performed on the various data types [strings, arrays, sets, objects, etc.]. This helps me to know what operations can be performed on the input data type. And this step will lead me to the next step.

Break the problem down

breakdown.jpg

Now that we have understood the problem thoroughly, we need to break down the solutions in a stepwise manner. Breaking the problem down helps us tackle it strategically. One important thing that can help us achieve this is to write pseudocodes.

What is a Pseudocode? A pseudocode is a step-by-step written outline of your code that you gradually transcribe into the programming language. It can also be defined as the implementation of an algorithm in the form of annotations and informative text written in plain English. It has no syntax like any of the programming languages and thus can't be compiled or interpreted by the computer.

pseudocode.webp

In this stage, we logically write down the steps that it takes to achieve the solution in ways that we can understand.

Another great tool that can help us is to visualize the problem. When we create logical and diagrammatic representations of the problems, the solutions become easy for us. This is because pictures are easier for our brains to comprehend than words. The picture superiority effect ensures we remember graphic information more readily than anything that we read.

Some other people love to draw flowcharts, and that is also a good idea if you are very conversant with it.

Code it out.

code.png

This is the next important step to carry out. After we have understood and analyzed our problems, the next thing is to begin to code them out. Many of the time, we know what to do next, but we don't know how to code it. This is why it is very good to familiarize ourselves with the methods and properties of various data types. What I do most of the time is to have the MDN docs for the various data types-Set, String, Array, Objects.

Don't forget to subdivide your code into smaller units. Always remember what format to return your data in and also in the format they want you to return the data in, and lastly, don't forget to test out your sample data. As this is the most important step that determines whether your solution is correct or not.

Optimize your code.

optimization.png This is one last step I love to take after writing my codes. I try to look out for errors in my codes and, most importantly, better ways I could have written certain operations. This step is important because most of the time when we see these problems, we just apply a brute force approach, i.e., we are not thinking about optimization or a better way to do it, we just want to arrive at the solution. Now that we have gotten the solution, we can now begin to apply our knowledge of the "Big O" to what we are doing to make sure that we are on point.

Many times, the truth is that we might forget certain steps and some particular syntax, and we might need the help of Google. Some platforms, like Coderbyte, provide you with a Google search bar to check out some quick things you might have forgotten due to tension or some other factors. Meanwhile, the search is limited to just the ranking page by Google, as you will not be able to visit the various web pages.

Basic Example

So I'll be working us through a basic example here so as to help us understand better:

Letter Capitalize In this challenge, the function LetterCapitalize(str) take the str parameter being passed and capitalizes the first letter of each word. Words will be separated by only one space.

Solution

Step 1:

First, read and understand the given question. After reading the question, we were required to read in a string input which is a sentence, and capitalize the first letter of each substring. For example, if the input string is hello world, our output should be Hello World

Step 2:

Second, we write our pseudocode:

1.  Split the given string into different words. Remember that the words will be separated by one space.

2.  Loop through each word and convert the first letter into uppercase. 

3.  Append the remaining part of the word to the capitalized letter

4.  Return the string back in the required format.

Through this pseudocode, we have understood our next line of action.

Step 3: Let's code it out:

NB: in this solution, I'll be using JavaScript to solve this problem.


const str = "christiana is an awesome lady";

function capitalizeFirstLetter(str){

  // first, we split the string into separate words based on the space.
  const arryWords = str.split(" ");

  const capitalizedWords = [];

    // for each word we capitalize the first letter and append the remaining part of the word to the capitalized letter
    for(let i = 0; i < arrayWords.length; i++){
        capitalizedWords[i] = arrayWords[i].charAt(0).toUpperCase() + arrayWords[i].slice(1)
    }
    const newString = capitalizedWords.join(' ');

    return newString;
}


const answers = capitalizeFirstLetter(str);

console.log(answers);

Output:

Christiana Is An Awesome Lady

Explanation

  • In the first step, we used the string method called split(" "). This method splits the string based on a space and pushes it into an array called arrayWords.

  • Next, we create an empty array to hold the capitalized words. (This step is optional as many would return the answers directly).

  • Next, we loop through each word in the arrayWords array, and as we loop through we perform two operations .

    • First, we take the first letter in the word using the string method charAt(0) and turn it into uppercase.
    • Second, we concatenate it with the remaining letters in the word using the slice(1) method.
  • Next, we join back the various words in the capitalizedWords array back using the .join('') and separate them with a space.

And with this, we have come to the end of this wonderful writeathon.

This article is an introduction to my new series where I would be publishing the solutions to coding challenges weekly. Please follow me here on Hashnode and don't forget to subscribe to my newsletter to get updates anytime a new article is released.

NB: This article was published as the #week4 article for the Hashnode #4articles4weeks writeathon.

I want to use this opportunity to appreciate Eleftheria Batsou and @hashnode for this opportunity and for bringing out the writer in me. I also want to appreciate my followers for their constant support and other writers for their wonderful content. Thank you all โคโค.

Follow me on Hashnode, LinkedIn, Twitter

ย