Algorithm: Palindrome. For the Javascript Coding Interview
By: Steven Paulino
Tags: JavaScript, Software Development, Palindrome, Algorithms, Coding Interviews

For the Javascript Coding Interview

Where the real fun begins

Teaching you common interview questions that you’ll see in a coding interview, and give you the tips and tools you need to ace your next coding interview.

Today's blog will be on a question that comes up from time to time in an interview setting. For those who prefer video, I made a video version of this blog. Let’s jump straight into it.

Directions

Given a string, return true if the string is a palindrome

or false if it is not. Palindromes are strings that

form the same word if it is reversed.

--- Examples:

palindrome("madam") === true

palindrome("abcdefg") === false

palindrome("racecar")

Fun right? There are many ways to tackle this problem and many directions this problem can go. Before you even touch that keyboard and begin to code out a solution, the first thing you will want to do is ask your interviewer clarifying questions to make sure you guys are on the same page.

What is considered a palindrome?

What about strings that contain spaces, casing, special characters?

Could I use built-in methods?

These types of questions are completely valid to ask and to be quite honest, the interviewer is hoping you ask these questions. Make no assumptions! If you were asked this question and jumped straight to coding, you aren't giving your interviewer insight or a chance to see what that beautiful noggin of yours is thinking.

Pseudocode

Now that you’ve asked some questions and got the gears turning, it is important to talk and list out the steps of how you're planning to write this algorithm.

- Going to accept the string

- clean the string if needed

- reverse the string in some way...

If you’re like me, the 3rd step usually is what requires the most brainpower! My thought process usually goes a little something like this: “Man I need to reverse this string but how do I do that exactly…if only there was a reverse method…oh, wait!”

Solution One

function palindrome(str) {

const reversed = str.split('').reverse().join('') return str === reversed;

} // palindrome("steven") --> false

// palindrome("racecar") --> true

This is probably one of the more straightforward compact ways to solve this. Reverse is not a valid method of the String.prototype but it is on arrays. The easiest way to convert a string into an array is by using the split method to split each character into its own element in an array. Once that is done, all you got to do is just call the reverse method on the array and join each character back together. Once that is done, we can do a simple equality check to see if the now reversed string is the same as the original string that was passed in.

Wrote out a working solution, now what?

Now that we got the first solution out the way, the interviewer may say in some fashion, “Could you solve this in another way?.” What they really mean is could you solve this problem without using the split, reverse, join methods.

Solution Two

function palindrome(str) {

for (let i = 0; str.length - i > i; i++) {

if (str[i] !== str[str.length - 1 - i]) return false;

}

return true;

}

This iterative solution loops through the string with essentially two-pointers and compares each letter individually. The loop will break when two characters don’t match.

Breakdown: str[i] !== str[str.length - 1 - i] We loop through the string

as we are looping, does the first letter match the last letter? if so, does the second letter match the second-to-last letter? etc...

I believe this solution is a good way to show your interviewer good control flow, and flex your handle of knowing which pieces of data you want when it logically makes sense.

Note:

The two solutions above ignore casing, spaces, and the like so dependent on your interviewer or what the question is specifically asking, sanitize the string to meet your needs.

Example: If a palindrome ignores spaces, casing, or punctuation.

//ignores just spacing

function palindrome(str){ const reversed = str.split('').join('') return str.replace(' ',"") === reversed.replace(" ","") } palindrome('taco cat')

OR //ignores casing,spacing, punctuation using a regular expression str.replace(/[^\w]/g,'').toLowerCase()

Hope this helps you on your next coding interview, good luck out there!