Facebook Interview Question for Software Engineers


Country: United States
Interview Type: Phone Interview




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

public boolean isPalindrome(String str) {
		
		if (str.length() <= 1)
			return true;
		return (str.charAt(0) == str.charAt(str.length() - 1)) && 	
			                isPalindrome(str.substring(1, str.length() - 1));
	}

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

u should put str.length()<=2 , please check your code with str="malyalam", it fails

- singhlrah April 18, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public boolean isPalindrome(String str) {
		
	if (str.length() <= 1)
		return true;
	return (str.charAt(0) == str.charAt(str.length() - 1)) 
		&& isPalindrome(str.substring(1, str.length() - 1));
}

- Simone April 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

u should put Str.length()<=2 , otherwise it will fail. Please check with Str value as "malyalam"

- rsingh0604 April 18, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

malyalam is not palindromr. malayalam is. can you check the input.

- Vib April 21, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

My solution in place and O(n) running time

/**
 * Created by sky on 16/04/15.
 */
public class InPlacePalindromeCheck {
    public static void main(String[] args) {
        String word = "A man, a plan, a canal, Panama";
        System.out.println(isPalindrome(word));
    }

    private static boolean isPalindrome(String word) {
        boolean ret = true;

        int i = 0;
        int j = word.length() -1;

        while (j > i) {
            if (!Character.isDigit(word.charAt(j))) {
                j--;
                continue;
            }

            if (!Character.isDigit(word.charAt(i))) {
                i++;
                continue;
            }

            if (word.charAt(i) != word.charAt(j)) {
                ret = false;
                break;
            }
            j--;
            i++;
        }

        return ret;
    }
}

- Felipe Cerqueira April 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static boolean isPalindrome(String s) {
	if (s == null || s.length() == 0) {
		return false;
	}
	if (s.length() == 1) {
		return true;
	}
			
	char[] c1 = s.toCharArray();
	int mid = c1.length/2;
	
	int i = mid - 1;
	int j = c1.length % 2 == 0 ? mid : mid + 1;
	
	while(i > 0 && j < c1.length && c1[i] == c1[j]) {
		i--;
		j++;
	}
	
	return (i == 0);
}

- guilhebl April 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

while(i >= 0....

you missed = above ?

- Vib April 21, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is done with a 2 pointer method, assuming you want the ENTIRE string to be a palindrome. That makes it quick and easy.

Just iterate forward from the start of the string, and backwards from the end of the string at the same rate, comparing the two pointers values at each iteration. This also automatically solves the odd-length edge case that can occur (which many people often write an extra conditional statement for)

In python:

def is_palindrome(word):
    for i in range(0, len(word)/2):
        if word[i] != word[len(word) - 1 - i]:
            return False
    return True

Runs O(n) time (actually n/2 but you get the idea) and uses no external data structures.

This also returns True for an empty string or 1 char string, as an empty or 1 char will always be symmetrical.

- Javeed April 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I should add that use of recursion or substrings only technically meets the requirements of the question, but is unnecessary and as always, recursion should be avoided if a solution of equal or better efficiency is available.

- Javeed April 16, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

test

- boreal April 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

BOOL IsPalin(char *pStr)
{
	UINT len = strlen(pStr);
	if (len == 1) return(TRUE);
	for (int i = 0, j = (len - 1); i < (len/2); i++, j--) {
		if (pStr[i] != pStr[j]) {
			return(FALSE);
		}
	}
	return(TRUE);

}

- boreal April 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is my solution insensitive to char case with test cases:

public class PalindromInplaceCheck {
	
	private static boolean isPalindrome(String text){
		int length = text.length();
		for(int i = 0; i < length; i++)
			if(Character.toLowerCase(text.charAt(i)) != Character.toLowerCase(text.charAt(length - i - 1)))
				return false;
		return true;
	}

	public static void main(String [] args){
		String testString1 = new String("common");
		System.out.println(testString1 + " is a palindrom: " + isPalindrome(testString1));
		String testString2 = new String("palIndromoRdniLap");
		System.out.println(testString2 + " is a palindrom: " + isPalindrome(testString2));
		String testString3 = new String("a");
		System.out.println(testString3 + " is a palindrom: " + isPalindrome(testString3));
		String testString4 = new String("");
		System.out.println(testString4 + " is a palindrom: " + isPalindrome(testString4));
		String testString5 = new String("abBa");
		System.out.println(testString5 + " is a palindrom: " + isPalindrome(testString5));
		String testString6 = new String("cdc");
		System.out.println(testString6 + " is a palindrom: " + isPalindrome(testString6));
	}
}

- denys.o.p April 21, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Shouldn't this break off at the midpoint to be more efficient?

- persie.joseph June 05, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

boolean isPalindrome(char[] w) {
        int s = 0;
        int e = w.length - 1;
        while (e >= s) {
            while (!Character.isAlphabetic(w[s]) && e > s) {
                s++;
            }

            while (!Character.isAlphabetic(w[e]) && e > s) {
                e--;
            }

            if (w[s] != w[e]) return false;
            s++;
            e--;
        }
        return true;
    }

- prosto.mail May 05, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String line = "A man, a plan, a canal, Panama";
line = line.replaceAll("[^a-zA-Z]", "");
line = line.toLowerCase(); // line is now = "amanaplanacanalpanama";
String ret = true;
while (j > i) {
            if (line.getCharAt(i) != line.getCharAt(j)) {
                ret = false;
                break;
            }
            j--;
            i++;
        }
        return ret;

- rade July 31, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The fastest check if it is palindrome from stdin in the west:

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>

#define PALINDROME_SIZE 100

int main(int argc, char *argv[])
{
»       char p[PALINDROME_SIZE], c;
»       size_t i = 0, j, half;
»       bool palindrome = true;

»       while ((c = getchar()) != EOF && c != '\n' && i < PALINDROME_SIZE)
»       »       if (isalpha(c))
»       »       »       p[i++] = tolower(c);

»       half = i / 2;

»       for (j = 0; j < half; j++) {
»       »       if (p[j] != p[i - 1 - j]) {
»       »       »       palindrome = false;
»       »       »       break;
»       »       }
»       }

»       printf("\t%s\n", palindrome ? "Palindrome" : "Not Palindrome");

»       return 0;
}

- ftonello January 31, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool isPalindrome(const char* str, int len) {
	int start=0; end=len-1;
	while(start<end){
		if(str[start] != str[end])
			return false;

		start++; end--;
	}
	return true;
}

- vandu October 28, 2017 | 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