## Facebook Interview Question Software Engineer / Developers

- 0of 0 votes
Write a program for Palindrome

**Country:**United States

**Interview Type:**Phone Interview

e = s.length() - 1 <--- computing s.length() is a O(n) complexity. if you put that in loop, then you will end up making an O(n^2) solution. Not many pic this detail but pointing this out just in case.

its easy when you go with string. But if provided with integer, then here comes the trick.

boolean prime(int n){

int length = 0;

int temp=n;

while( temp!=0 ) {temp/=10;length++;}

int i=0;

while(n!=0 ){

int lsd = n%10;

int msd = (int)(n/Math.pow(10,length-1));

if( lsd == msd ){

n %= Math.pow(10,length-1);

n /= 10;

length -= 2;

}else

return false;

}

return true;

}

It must work for numbers:

```
for (i=n;i>9;i=n)
{
a= n/10;
b= n%10;
printf("%d",b);
n=a;
}
printf("%d",n);
getch();
}
```

```
#!/usr/bin/python
def isPalindrome(s):
for i in range(len(s) / 2):
if s[i] != s[len(s)-i-1]:
return False
return True
s = raw_input("Enter string: ")
print(isPalindrome(s))
```

I'd like to add a little thing to make this case insensitive:

```
#!/usr/bin/python
def isPalindrome(s):
for i in range(len(s) / 2):
front_char = s[i].lower()
back_char = s[len(s)-i-1].lower()
if front_char != back_char:
return False
return True
s = raw_input("Enter string: ")
print(isPalindrome(s))
```

```
public static void main(String[] args) {
assert isPalindrome("abbbcbbba");
assert isPalindrome("aba");
assert isPalindrome("aa");
assert isPalindrome("a");
assert isPalindrome("");
assert !isPalindrome("ab");
assert !isPalindrome("abc");
}
private static boolean isPalindrome(String str) {
int l = 0, r = str.length() - 1;
while (l < r) {
if (str.charAt(l++) != str.charAt(r--)) {
return false;
}
}
return true;
}
```

- Anonymous on April 12, 2012 Edit | Flag Reply