Facebook Interview Question for Software Engineers


Country: United States
Interview Type: Phone Interview




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

It is impossible to solve this problem in O(n) time as the number of solutions is of the exponential order.

- technoapurva September 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What can be the best complexity ,is it O(n*n!) ??

- dev123 September 24, 2015 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 votes

It will be O(n!).

- technoapurva September 24, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Actually, if there are duplicates, the best we can do is O(n!/((n_1!)(n_2!)...(n_m!))) Where n_1 through n_m are the number of occurrences for each given non-distinct (duplicate) element in the string.

- effy October 15, 2015 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

The definition of a permutation is p(n,k) = n!/(n-k)! and if k=0 then the answer is n! thus the solutions can never be O(n) if coded optimally.

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

import java.util.List;
import java.util.ArrayList;

class Permutations {
	
	private static List<String> permutations(String s) {
		List<String> permutations = new ArrayList<String>();
		if (s == null) {
			return null;
		} else if (s.length() == 0) {
			permutations.add("");
			return permutations;
		}
		
		List<String> remainder = permutations(s.substring(1));
		for (String permutation : remainder) {
			for (int i = 0; i <= permutation.length(); ++i) {
				permutations.add(insertCharAt(permutation, s.charAt(0), i));
			}
		}
		return permutations;
	}
	
	private static String insertCharAt(String s, char c, int i) {
		return new StringBuilder(s).insert(i, c).toString();
	}
		
}

O(N!) time and space. The problem cannot be solved in linear time because we have N! solutions (as Technoapurva said).

- Iuri Sitinschi September 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@luri- Isin't this also n*n! ?

- guest September 26, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

isn't this soln - n*n! ?

total permutations = n! and while calculating every permutation u r traversing over permutation length while inserting element in between..

- Source September 26, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Isn't this algo n*n! ?

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

*
 * @author akshay anand
 */
public class SUBsTRING {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        
        String str = "tesw";
        // find all permutations
        //    permute(s);
        Set<String> s = new HashSet<String>();
        
       s =  test(str,s);
       
       System.out.println(s);
        
        
}

    public static Set<String> test(String str, Set s){
        if(str.length()==1){
             s.add(str);
            return s;
        }
        if(str.length()==2){
            s.add(str.charAt(0)+""+str.charAt(1));
            s.add(str.charAt(1)+""+str.charAt(0));
            
            return s;
        }
        
        else
        {
            for(int i=0;i<str.length();i++){
                String s1 = str.substring(0,i);
                String s2 = str.substring(i, str.length());
            
                s.add(s1+""+s2);
                s.add(s2+""+s1);
                s= test(s1,s);
                s= test(s2,s);
               
            }
        }
        return s;
    }

- permutation of string September 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

*
* @author akshay anand
*/
public class SUBsTRING {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        
        String str = "tesw";
        // find all permutations
        //    permute(s);
        Set<String> s = new HashSet<String>();
        
       s =  test(str,s);
       
       System.out.println(s);
        
        
}

    public static Set<String> test(String str, Set s){
        if(str.length()==1){
             s.add(str);
            return s;
        }
        if(str.length()==2){
            s.add(str.charAt(0)+""+str.charAt(1));
            s.add(str.charAt(1)+""+str.charAt(0));
            
            return s;
        }
        
        else
        {
            for(int i=0;i<str.length();i++){
                String s1 = str.substring(0,i);
                String s2 = str.substring(i, str.length());
            
                s.add(s1+""+s2);
                s.add(s2+""+s1);
                s= test(s1,s);
                s= test(s2,s);
               
            }
        }
        return s;
    }

- permutation of string September 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

*
 * @author akshay anand
 */
public class SUBsTRING {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        
        String str = "tesw";
        // find all permutations
        //    permute(s);
        Set<String> s = new HashSet<String>();
        
       s =  test(str,s);
       
       System.out.println(s);
        
        
}

    public static Set<String> test(String str, Set s){
        if(str.length()==1){
             s.add(str);
            return s;
        }
        if(str.length()==2){
            s.add(str.charAt(0)+""+str.charAt(1));
            s.add(str.charAt(1)+""+str.charAt(0));
            
            return s;
        }
        
        else
        {
            for(int i=0;i<str.length();i++){
                String s1 = str.substring(0,i);
                String s2 = str.substring(i, str.length());
            
                s.add(s1+""+s2);
                s.add(s2+""+s1);
                s= test(s1,s);
                s= test(s2,s);
               
            }
        }
        return s;
    }
    /*end*/

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

public class Main {
    private static void permutation(String prefix, String str){
        int n = str.length();
        if (n == 0) 
            System.out.println(prefix);
        else {
            for (int i = 0; i < n; i++)
                permutation(prefix + str.charAt(i), 
            str.substring(0, i) + str.substring(i+1));
        }
    }
    public static void main(String[] args) {
        permutation("", "ABCD");
    }
}

- havoc October 03, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def permute_string(the_string):
    string_list = list(the_string)
    permute_string_internal('', 0, string_list)


def permute_string_internal(prefix, start_index, string_list):
    if start_index < len(string_list):
        char_at_start_index = string_list[start_index]
        for index in range(start_index, len(string_list)):
            char_at_index = string_list[index]
            string_list[index] = char_at_start_index
            string_list[start_index] = char_at_index
            permute_string_internal(prefix + string_list[start_index], start_index + 1, string_list)
            string_list[start_index] = char_at_start_index
            string_list[index] = char_at_index
    else:
        print prefix

- rizTaak October 18, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Below is the c++ code.

///
vector<string> permutations(string& str, const size_t idx = 0)
{
vector<string> result;
if (idx == str.length()) {
result.push_back (str);
}

for (size_t i = idx; i < str.length(); ++i) {
swapChar(str, idx, i);
result.push_back(permutations(str, idx + 1);
swapChar(str, idx, i);
}
return result;
}
\\\

- Rohit J December 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Recursive and iterative solutions:

private static void permutationsRecursive(String letters) {
        countRecursive= 0;
        
        permutationsRecursive(letters, "");
    }

    private static void permutationsRecursive(String letters, String wordUntilNow) {
        if (letters.length() > 0) {
            for (int i = 0; i < letters.length(); i++) {
                String newWordUntilNow = wordUntilNow + letters.charAt(i);
                String newLetters = remove(letters, i);
                permutationsRecursive(newLetters, newWordUntilNow);
            }
        }
        else {
            System.out.println(wordUntilNow);
        }
    }

    private static final Hashtable<Integer, Long> factorials = new Hashtable<Integer, Long>();
    private static long factorial(int n) {
        long fac = 1;
        while (n > 0) {
            fac *= n--;
        }
        return fac;
    }

    private static void permutationsIterative(String letters) {
        int size = letters.length();
        long numberOfPermutations = factorials.get(size);
        for (long
        countIterative = numberOfPermutations; i = 0; i < numberOfPermutations; i++) {
            String st = letters;
            StringBuilder sl = new StringBuilder();
            for (int j = size - 1; j >= 0; j--) {
                int index = (int)((i / factorials.get(j)) % (j+1));
                sl.append(st.charAt(index));
                st = remove(st, index);
            }
            System.out.println(sl.toString());
        }
    }

- Lucidio August 14, 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