Microsoft Interview Question

Write a method to compute if a string is a palindrome, disregarding spaces.

Interview Answers

Anonymous

Nov 7, 2012

isPalidrome(String s) { removeSpaces(s); String r = reverseString(s); if (r == s) then return True; Otherwise return False; }

Anonymous

Nov 7, 2012

bool isPalindrom(char s[]) { int start = 0, length, end; length = end = strlen(s); if(end<2) { return false; } end--; while(start

Anonymous

Nov 13, 2012

In Java: private static boolean isPalindrome(String str) { int leftIndex = 0; int rightIndex = str.length() - 1; while(leftIndex < rightIndex) { if (str.charAt(leftIndex) == ' ') { leftIndex++; continue; } if (str.charAt(rightIndex) == ' ') { rightIndex--; continue; } if (str.charAt(leftIndex) != str.charAt(rightIndex)) { return false; } leftIndex++; rightIndex--; } return true; } N/2 iterations for characters + N iterations for whitespaces = O(N) complexity