function isPalindrome(str) {
return (str.split("").reverse()).join("") == str;
} function isPalindrome(str) {
return (str.split("").reverse()).join("") == str;
} function isPalindrome(str) {
for (let i = 0; i < str.length / 2; i++) {
if (str[i] !== str[str.length - i - 1]) {
return false;
}
}
return true;
}
Quick check list:1. Knows about es6
2. Knows about === and !==
3. O(n) efficiency with no memory bloat
4. Didn't write 30+ lines on a trivial function
I won't even add a comment about unicode since it should be your default assumption when working with javascript that multi-byte characters and surrogate pairs don't work properly.
You need to ignore white space and punctuation.
6. Didn't include any tests
7. Bonus! This function also returns true if you pass it a palindromic array! Er... Maybe it shouldn't do that.
This function is too simple to expect anyone to ask you any follow up question regarding the "spec" unless they are trying to game the interview.
Also note that the duct typing properties of JS are often used to allow such string/array ambiguities.
Seems like we could find out plenty about how a candidate thinks and codes by pursuing this line of questioning. So long as the candidate doesn't respond to these questions by saying "Look, this is stupid, I'm not going to dig into requirements, that seems like I'd be trying to game the interview. This is my final answer - it tests palindromes. Next question.".
• Will this function get only strings, or other stuff too?
• Do punctuation and capitalization count?
• Are there any special cases I should worry about?
def is_palindrome(str):
return str == str[::-1]So your solutions works after a preprocessing step.
let is_palindrome str = str = String.rev str ;;
(function is_palindrome will return true or false for input str )