Meta interview question

Write a palindrome-checking function

Interview Answers

Anonymous

Nov 19, 2012

This has a N/2 complexity : function isPalyndrome($str) { $array = str_split($str); $size = count($array); $pivot = floor($size / 2); for($i = 0; $i < $pivot; $i++) { if($array[$i] != $array[$size - $i - 1]) { return false; } } return true; }

2

Anonymous

Dec 29, 2012

#include #include using namespace std; bool IsPalindrome(const string& input) { for (int i = 0; i < input.size()/2; ++i) { if (input[i] != input[input.size()-i-1]) { return false; } } return true; } int main() { const string str("madam"); const string str1("madama"); cout << IsPalindrome(str) << endl; cout << IsPalindrome(str1) << endl; return 0; }

Anonymous

Nov 15, 2012

My solution walked char pointers back from the end and forward from the beginning of the string, returning true if the crossed over and false if the two chars pointed to didn't match.