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

Pangrams are sentences constructed by using every letter of the alphabet at least once.

like: "The quick brown fox jumps over the little lazy dog"

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

The quick brown fox jumps over the little lazy dog


}

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

For some reason I couldn't copy my code here properly in previous attempt and I don't see an option of edit.

public class Pangrams {
	
	public static boolean func(String str) {
		Set<Character> alphabets = new HashSet<>();
		for(int i=0;i<26; i++) {
			alphabets.add((char) ('a' + i));
		}
		
		char[] allchars = str.toCharArray();
		
		for(int j=0; j<allchars.length; j++) {
			alphabets.remove(allchars[j]);
			if(alphabets.isEmpty()) return true;
		}
		
		return false;
		
	}
	
	public static void main(String[] args) {
		System.out.println(func("The quick brown fox jumps over the little"));
		
	}

}

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

I'm assuming your question, which isn't a question by the way you said a word not sure what you expect of us please add more detail next time, is to find if a sentence is a pangram (contains at least 1 of every letter in the alphabet)

To do so, create a set containing all the letters, then remove each one if it is seen in the string.

In python:

def is_pangram(word):
    alphabet = set(["a", "b", "c", "d", "e", "f", "g", "h",
                        "i", "j", "k", "l", "m", "n", "o", "p",
                        "q", "r", "s", "t", "u", "v", "w", "x",
                        "y", "z"])
    for x in word.lower():
        if x in alphabet:
            alphabet.remove(x)
    return not alphabet

The logic is that, for each character, if you see it in the alphabet set, it wasn't seen before, so remove it, indicating it has been seen. If the character isn't in the set, it either hasn't been seen yet, or it's not a letter (so, a numeric value or symbol)

Some people have argued that this can be done by creating an empty set, then filling it as you go along, since sets in python will not duplicate entries, then checking that the size of the set is 26. When faced with the possibility of a number in the string, some suggest using ascii numeric conversion of characters to determine if they are letters, but the end result is that the exact same space is used, (the method I wrote starts with 26 and ends with none, the other method starts with none and ideally ends with 26) but containes extra logic that isn't necessary.

Runs in O(n) when using a lookup data structure with O(1) lookup time.

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

// O(n) run time O(1) space if only alphabets are used.

public boolean isPangram(String s){
		s = s.toLowerCase();
		int charMap = 0x03FFFFFF;
		
		for(int i=0;i<s.length();i++){
			char c = s.charAt(i);
			if(c != ' ' && isNthBitSet(charMap,(int)(c-'a'))){
				charMap = resetNthBit(charMap,(int)(c-'a'));
			}
		}

		return charMap==0;
	}
	
	private boolean isNthBitSet(int number,int n){
		int flag = 0x02000000;
		return (number&((flag)>>>n)) >0;
	}
	
	private int resetNthBit(int number,int n){
		int flag = 0x02000000;
		return number^((flag)>>>n);
	}

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

1) Keep an array of size 26
2) Iterate through every letter in sentence
3) Mark corresponding location in array to 1
int val = 0;
if(str[i] >=97 && str[i] < 123)
{
val = 97;
}
else if (str[i] >= 65 && str[i] < 91)
{
val = 65;
}
else
{
continue; // Do nothing
}
arr[str[i] - val] = 1;

4) Make sure all values in arr is set to 1

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

1)Create Set of Character data type.
2) Read every characters in the String, Add it in Set.
3) Iterate throughout the String.
4)Set do not allow duplicates so one entry exist for one character if it exists on the string.
5)Check the size of Set.
6)If the size is 26 then it is a Pangram else it is not pangram.

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

public class Panagram{

	//T(n) = O(n); // n is the length of the string 
	//S(n) = O(n);
	//Assumes that input string has only ASCII characters  
	public static boolean isPanagram(String str){
		int [] counts = new int[256];
		char [] charArr = str.toCharArray();
		for(int i = 0; i < charArr.length; i++){
			if(charArr[i] >= 97 && charArr[i] <= 122){
				charArr[i] -= 32;
			}
			counts[charArr[i]]++;
		}
		for(int i = 65; i <= 90; i++){
			if(counts[i] < 1){
				return false;
			}
		}
		return true;
	}
	

	public static void main(String[] args) {
		System.out.println(Panagram.isPanagram("The quiCk brown fox jumps over the little lazy dog"));
	}

}

- coolguy September 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

create an int array A, of size 26 and initialize to 0
for each letter S[i], in given string, if it is alphabet, then A[lower(S[i])-'a']++
iterate over A and return false if you find any A[i] equal to 0

- Pragalathan M September 24, 2015 | 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