## Facebook Interview Question for Software Engineer / Developers

Country: United States
Interview Type: Phone Interview

Comment hidden because of low score. Click to expand.
1
of 1 vote

``````boolean isPalindrome(String s) {
if (s == null || s.length() == 0) {
return false;
}
for (int s = 0, e = s.length() - 1; s < e; ++s, --e) {
if (s.charSt(s) != s.charAt(e)) {
return false;
}
}
return true;
}``````

Comment hidden because of low score. Click to expand.
0

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.

Comment hidden because of low score. Click to expand.
0

No, s.length() is O(1) in java. However, it's true for strlen(s) in C

Comment hidden because of low score. Click to expand.
1
of 1 vote

Is it that simple at Facebook ?

Comment hidden because of low score. Click to expand.
0

You would think so would you. Are these "official" Facebook questions? Presumptuous at best!

Comment hidden because of low score. Click to expand.
0
of 0 vote

boolean palinCheck(String s)
{
if(s.length()<=1)return true;
else if(s.charAt(0)==s.charAt(s.length()-1)))
return true&&palincheck(s.substring(1,s.length()-1));
else return false;

}

Comment hidden because of low score. Click to expand.
0
of 0 vote

Palindrome for what? string / array(int etc...) / linked list?

Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

For number just reverse the number and check if they are equal.
rev = 0;
while ( num != 0 ) {
rev = rev*10 + num%10;
num=num/10;
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

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();
}``````

Comment hidden because of low score. Click to expand.
0

Of course, n is the number.<integer> :)

Comment hidden because of low score. Click to expand.
0

You'll get the reverse number by this.. print the original number before it to create the palindrome......

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````#!/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))``````

Comment hidden because of low score. Click to expand.
0

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))``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````#include <iostream>

int main()
{
std::cout << "1234321" << std::endl;
return 0;
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````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;
}``````

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.