Facebook Interview Question for Software Engineers


Country: United States
Interview Type: Phone Interview




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

void print_permutations(const string &s) {
  auto t = s;
  sort(t.begin(), t.end());
  while (true) {
    cout << t << endl;
    if (!next_permutation(t.begin(), t.end())) {
      break;
    }
  }
}

- Anonymous May 25, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

This question has been asked several times... Cost O(n!)

def string_permutations(s):
    if len(s) == 1: return s
    
    solutions = []
    for x in xrange(len(s)):
        solutions.extend(map(lambda r: s[x] + r,
                             string_permutations(s[:x]+s[x+1:])))
    return solutions

- Fernando May 26, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

public class TestPermutation {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String input = "abc";
		int setPos[] = new int[input.length()];
		getPermutation(input, "", setPos);
	}

	public static void getPermutation(String input, String output,
			int[] setPos) {

		if (null == input | null == output) {
			return;
		}

		if (output.length() == input.length()) {
			System.out.println(output);
			return;
		}

		StringBuilder s = new StringBuilder();
		s.append(output);
		for (int i = 0; i < input.length(); i++) {
			if (setPos[i] == 0) {
				s.append(input.charAt(i));
				setPos[i] = 1;
				getPermutation(input, s.toString(), setPos);
				setPos[i] = 0;
				s.deleteCharAt(output.length());
			}
		}

	}

}

- Anonymous June 03, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A naive recursionless code is possible:

/* 
A nice trick to find permutations is use integers with base b.
Suppose there is a string of length b, with all unique characters.
Now, that means, the string can be represented as a base b integer.
Permutaions, of the string can be now found by incrementing the number, 
such that all digits are unique.
Now a demonstration:
abc : 3 chars.
3 digit number, and base 3.
The digits are: a:0, b:1, c:2
012 021 102 120 201 210 
--> map_back to -->
abc acb bac bca cab cba 
and we are done.
This can be easily implemented:
*/
def map_back(encoded, string){
  str( encoded.value ,'' ) -> { string[int($.o)] }
}

def permutations( string ) {
    // imagine all chars are unique, else there will be repeatation 
    b = size(string)
    // start with min.
    min_str = '0' + str( [1:b] , '' ) -> { str($.o,b) }
    max_str = min_str ** -1
    min = int(min_str,b,0)
    max = int(max_str,b,0)
    perms = list()
    perms += map_back(min_str,string) 
    for ( x : [ min + 1 : max  ] ){
      str_x = str(x,b)
      if ( size(str_x) < b ){ str_x = '0' + str_x }
      if ( size( set(str_x.value) ) == b ){
          // all different - map it 
          perms += map_back(str_x,string)
      }  
    }  
    perms += map_back(max_str,string) 
}
p = permutations( "abc" )
println(p)

- NoOne May 26, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Permutations {
	public static void findPermutations(String str){
		permutations("",str);
	}
	
	public static void permutations(String prefix, String str){
		if(str.length()==0){
			System.out.println(prefix);
		}
		for(int i =0;i<str.length();i++){
			permutations(prefix+str.charAt(i),str.substring(0,i)+str.substring(i+1,str.length()));
		}
		
	}
	
	public static void main(String []args){
		findPermutations("abc");
	}
	

}

- mail.smritiraj May 26, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

std::vector<char> out;
std::queue<char> in;

void permute(std::queue<char> &in, std::vector<char> &out) {
    // take from the front, add to out, and then push back into the queue
    if (in.empty()) {
        for(std::vector<char>::iterator it = out.begin(); it != out.end(); ++it) {
            cout << *it;
        }
        cout <<"\n";
        return;
    }
    
    for(int i = 0; i < in.size(); i++) {
        out.push_back(in.front());
        in.pop();
        permute(in, out);
        // the element is not in the in queue. 
        in.push(out.back());
        out.pop_back();
    }
    
}

int main() {
    std::string input("abcd");
    for(int i = 0; i < input.length(); i++) {
        in.push(input[i]);
    }
    permute(in, out);
	// your code goes here
	return 0;
}

- ali.kheam May 26, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

or you can use the backtracking with recursion

void swap(string &str, int a, int b) {
    if (a==b){
        return;
    }
    char k = str[a];
    str.replace(a, 1,1, str[b]);
    str.replace(b, 1,1, k);
}

// backtracking
void permu(std::string &str, int index) {
    if (index == str.length()) {
        cout <<str <<"\n";
        return;
    }
    for(int i = index; i < str.length(); i++) {
        swap(str, i,index);
        permu(str, index+1);
        swap(str, index, i);
    }
    
}

- ali.kheam May 26, 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