Hmm..the Palindrome

Rashad Walcott
Rashad’s Tech Blogs
2 min readJan 15, 2020

Early December I attended a hiring event and did couple interviews with different companies. One of those interviews went really great as the other left me stumped. I did not exactly get very deep into studying Algorithms and Data Structures but was presented with a problem and had to give a proper solution.

So I was asked if I knew what a Palindrome was and at that time I did not but the interviewer explained it to me and I understood what was being asked. A Palindrome is “a word, phrase, or sequence that reads the same backward as forward”. I felt as though I came up with some solid solutions on the fly but I think the interviewer wanted more.

I have been working on a Udemy course studying Algorithms and I wanted to share with you all in case someone has not heard of a palindrome. So if presented with “Given a string, return true if the string is a palindrome or false if it is not”. These are two solutions that could solve this problem.

function palindrome(str) {
return str.split('').every((char, index) => {
return char === str[str.length - index -1];
})
}

y You can take the string that is passed in directly and split it in order to use the .every method. The only thing I would say about this solution is that it compares every element in the array with each-other. For this I think the better solution for this problem would be.

function palindrome(str) {
const rev = str.split('').reverse().join('');
return str === rev;
}

This solution takes the string passed into the argument and saves it into a variable and compares the reversed version to the version passed in. However, in an interview if they did not want a simple solution as the second one the first solution would be what you could use to show something different.

Hope you enjoyed and this helped someone who may not have known about palindromes such as myself. 😁

--

--