Palindrome

Tyler Hueter
4 min readSep 27, 2019

--

Part #1 in a series of frequently asked technical interview questions.

As I continue my journey as a software developer, I am faced with the ever daunting technical interview. (For any employers reading this, I much prefer the code challenge!) So to help anyone else following this path, I decided to do a series of blog posts reviewing some frequently seen problems. I will be focusing on answering questions using Javascript.

For the first in the series I want to look at the question:

“Determine if a given input is a palindrome.”

First, what is a palindrome? Oxford dictionary defines it as “a word, phrase, or sequence that reads the same backward as forward.” Notable examples are madam, race car or the phrase “Never Odd Or Even.” Now that we know what a palindrome is, let’s establish a couple constraints.

  1. The given input will always be a string.
  2. input.length > 1

As with any problem there are several ways it can be solved. I am going to focus on one path using three different methods to quickly test any given input. To solve this problem we are going to want to take our original input and reverse it then test to see if it matches the original. This can also be solved using recursion but that will be for another post.

First let’s establish our input and begin writing our test function.

We have now defined our input and our function. Next we want to reverse our input. Javascript has this very nifty built in method .reverse() that will do just that for us! Just one catch, that method only works on arrays. So now we just have to transform our string into an array. To do this we will use the .split() method.

Note: pay attention to the “” that is passed into the .split() method call. If you leave out the “” marks newArray =[“some string”]. Also if you add a space , “ “, newArray = [“some”, “string”]. This will come up again later.

Our original input has not been altered, but a new array has been created from it. Now we can use the .reverse() method to flip everything around. Note that while .split() created a new array and did not alter the original string, .reverse() does change the array it is called on.

Ok, getting closer but now if we have to change this array back to a string so we can actually compare two objects of the same data type. To do this we need to define a new variable and call the .join() method on our array. Just like .split, .join does not alter the original object it is called upon.

Note the same use of “” as with the .split method.

Now all that’s left to do is compare these two strings and if they are equal return a true for a positive result of our palindrome test.

Let’s break it down step by step:

Now let’s test it in our console with an actual palindrome:

Great! But what if we try it on “Madam” or “Never Odd Or Even”? We are now going to have to modify our code to cover some edge cases. First let’s take care of those capital letters because “Madam” and “madaM” are not the same. To do this we will modify our input with the .toLowerCase() method.

Note: we also adjusted our code to replace the str variable with modifiedString.

Ok, now we have solved this for inputs that include capital letters. To handle phrases we will need to incorporate the .split() and .join() methods again. This time we will need to adjust the “”’s we pass in to make sure we remove the spaces between words.

And…quick console test

It works! But that is some ugly code. Javascript allows us to chain methods together. It can sometimes make it a bit harder to follow, but let’s see if we can clean this up a bit.

Now, that is a bit cleaner and easier to read. We are done. If you want, see if you can reduce this even further using the ability to chain methods. Remember there are always more than one way to solve a problem, and here we just reviewed one of them. That wraps up this interview question review, I will be posting more in the coming weeks. If there is a particular problem you would like to see reviewed please let me know.

--

--