4 ms·
I've had this question and replied with: const isPalindrome(str) { return str === str.split('').reverse().join(''); } And the post interview notes said bad
by CryoLogic 8y ago
I've had this question and replied with:
const isPalindrome(str) {
return str === str.split('').reverse().join('');
}
And the post interview notes said bad performance on the isPalindrome() question since I didn't write out my own functions (interviewer did not mention to).
- calcifer 8y agoPersonally, if you gave me this answer I would accept it and then asked you how those functions work and how you would implement them. Still trivial, but it would tell me whether you are familiar with the basics or you just memorized some stdlib functions.
- Bahamut 8y agoTo be fair, if you wanted to do a performant implementation, this is probably a more expensive implementation. The expected response is generally for you to iterate over the string and construct a count map of how many times a character appears in a string, and then iterate over the keys of that object and have at most one odd number in the values of the count map. The interviewer probably should have probed you about performance though if their intention was to judge you on that (they're both O(n) time complexity, but split, reverse, and join are generally more intensive).
- wheresvic1 8y agoErr I always thought that the expected performant response is to have two indexes 0 and length-1 and bring them to the middle checking that the values are equal :)
- glangdale 8y agoI'm very confused by this answer. Have I missed a joke? Wouldn't the expected response be to iterate over half the string comparing the characters to the other half (in reverse order)? The criteria you provide seems to suggest aabbc is a palindrome.
- Bahamut 8y agoAh sorry, I misunderstood the question - the one I've seen usually is if a string could be rearranged as a palindrome ha.
- dlgeek 8y agoYou're thinking of an anagram, not a palindrome.
- monksy 8y agoMost of the time when they ask for the palindrome questinon they prevent you from using StringBuilder.reverse.