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

- Anonymous April 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- A March 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Kevin August 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Is it that simple at Facebook ?

- Orkut April 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- FBNerd February 26, 2013 | Flag
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;



}

- Shashi April 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Anonymous April 12, 2012 | Flag Reply
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;
}

- virat April 13, 2012 | Flag Reply
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;
}

- Raj May 11, 2012 | Flag Reply
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();
}

- Bihan Sen May 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Bihan Sen May 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Bihan Sen May 23, 2012 | Flag
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))

- took_two_seconds August 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- took_two_more August 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>

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

- clow July 21, 2013 | Flag Reply
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;
}

- Safi December 11, 2014 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More