Algorithmic Problem Solving - Step by Step
Tags: javascript, algorithms, tutorial, beginners
Published: 2021-11-18 15:16:36

Let's be real - in the current job market most employers want to know that you can not only learn a coding language, but also apply it while actively problem solving. In walks algorithm problems (or also fondly know as _algos_) which are often used as a tool for employers to filter for exactly what they're looking for. At first glance, algorithm problems can be intimidating! The secret to tackling these is going step by step.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eo3dsjrxonnjz17gnova.jpg)

## Steps to algorithmic problem solving

Google often has a lot to tell us. One of the many things is how to go about solving a problem. There are four recommended steps: _analyze, implement, experiment, design._ Lets go through these four steps and talk through what exactly is being recommended. 

#### Analyze
Read through the problem presented. What is the problem being presented? Are there any specifications being listed/stated? If it's easier to separate the problem into sections, do that! Try stating the problem in your own words to solidify that you understand the problem. 

#### Implement
We have a firm grasp of the problem at hand. Now, how would we go about solving this problem? What steps do we need to take to solve the problem? Write line by line (similar to how you would write out your code) what exactly you need to do to get the desired outcome. 

#### Experiment
This is the fun part - write out your own test cases for the code you'll eventually write. Include what you expect the return to be as well. Think through what you have written out so far, is there a test that would break your code? Include that too. Coding can be fun - embrace the unknown. 

#### Design
Thus far, we've read through the problem and grasped what is being presented to us. Then we've written out what we expect  to code out in JavaScript as well as made test cases. Now begin to actually code out the solution. Remember that the code may fail the test cases, try experimenting with the code and tests to see where the issue may be. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yxtlsiq753r2hhdzk2vf.jpg)

## Our own algorithmic problem to solve

Here's our problem: 

Confirm whether or not a word is a palindrome.

For example:

```
> isPalindrome('kayak')
= true

> isPalindrome('cheetah')
= false

> isPalindrome('tenet')
= true
```

**Analyze** 
The current problem is asking for a function that will confirm whether or not a string is the same both backwards and forwards. The example it gave is true or false which also implies that this will involve comparing the string at some point in my code.  For our use, let's assume that it will only give single word strings and that case doesn't matter or all strings are lowercase.

**Implement**
Based on the problem's expected answer I'll probably attempt to solve the algorithm like so: 
* Create a function that takes a string as an argument
* Reverse the string (using built-in javascript methods like `.split()` and `.reverse()` could make this quicker but we're going to go the long way this time) so we'll need to run a for loop that iterates through the string
* Compare the initial string to the same string that is reversed using an if statement

**Experiment**
Let's make test cases with expected results! For now, I'll only list the test cases, but think through what you think will work. There's a couple palindromes that are actually two words instead of one, something that isn't accounted for in the hard code. Let's find out what the code does. 

```
> isPalindrome("madam")
> isPalindrome("tacos")
> isPalindrome("trees")
> isPalindrome("race car")
> isPalindrome("racecar")
> isPalindrome("taco cat")
> isPalindrome("tacocat")
```

**Design**
After rereading the problem and how it could be solved this is what should work:
```
function isPalindrome(str){
  let long = str.length;
  for(i = 0; i < long/2; i++){
    if (str[i] !== str[long -1 -i])
       return false;
  }
  return true;
}
```
Here's our results:
```
> isPalindrome("madam")
  // expect true
= true

> isPalindrome("tacos")
  // expect false
= false

> isPalindrome("trees")
  // expect false
= false

> isPalindrome("racecar")
  // expect true
= true

> isPalindrome("taco cat")
  // expect false
= false

> isPalindrome("race car")
  // expect false
= false

> isPalindrome("tacocat")
  // expect true
= true
```
It works!

If using the built in methods from javascript we could do something shorter:
```
function isPalindrom(str) {
    return str == str.split('').reverse().join('');
}
```

That's an algorithm problem from start to finish. Though intimidating, they are definitely possible to tackle. Remember - analyze, implement, experiment, and design make breaking down the problem into bite size pieces easier. 

Good luck!