Facebook Interview Question for Software Engineers


Country: United States
Interview Type: In-Person




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

calculate even, odd sum(ascii) and compute a hash for it. Answer will the number of different hashes generated.

- Popeye August 10, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

from collections import Counter
def count(arr):
    fs = [(tuple(Counter(x[::2]).items()), 
    	     (tuple(Counter(x[1::2]).items()))) 
    	     for x in arr]
    return sum(z for (x,z) in Counter(fs).items() if z>1)
    
print count(["abcd","cbad","bacd"])
print count(["abcd", "acbd", "adcb", "cdba",
"bcda", "badc"])

- adr August 09, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class SpecialEquivalient {

    private int count = 0;

    SpecialEquivalient (String[] input) {
        Set<String> outputSet = new HashSet<String>();;
        for(String str : input) {
            String[] evenOdd = splitEvenOdd(str);
            String sortedString = sortString(evenOdd[0]) + sortString(evenOdd[1]);
            outputSet.add(sortedString);
        }
        count = outputSet.size();
    }

    String[] splitEvenOdd(String str) {
        char[] chars = str.toCharArray();
        String odd = new String();
        String  even = new String();
        for (int i=0; i<str.length(); i++) {
            if (i % 2 == 1) {
                odd += chars[i];
            } else {
                even += chars[i];
            }
        }
        return new String[] {odd, even};
    }

    String sortString(String str) {
        char[] charArray = str.toCharArray();
        Arrays.sort(charArray);
        return new String(charArray);
    }

    public int uniqueCount() {
        return count;
    }
}

- Sachin Magdum August 19, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Main {

    public static void main(String[] args) {

        // test set
//        String[] arr = {"abcd", "cbad", "bacd"};
        String[] arr = {"abcd", "acbd", "adcb", "cdba", "bcda", "badc"};

        // convert ArrayList form as it is beneficial to handle
        ArrayList<String> input = new ArrayList<String>();
        for (int i = 0; i <arr.length; i++) {
            input.add(i, arr[i]);
        }

        // main loop
        for (int i = 0; i < input.size()-1; i++) {

            for (int j = i+1; j < input.size(); j++) {

                // Question here: any possibility that identical strings exist? (assuming no for now)

                // check with swapEven
                if (input.get(i).equals(swapEven(input.get(j)))) {
                    // remove if equivalent
                    input.remove(j);
                    j--;
                    // go back to loop
                    continue;
                }

                // check with swapOdd
                if (input.get(i).equals(swapOdd(input.get(j)))) {
                    // remove if equivalent
                    input.remove(j);
                    j--;
                    // go back to loop (no need as this is end of loop)
                    //continue;
                }
            }

        }

        System.out.println("length: " + input.size());
    }

    // swap a character at even-numbered index
    // assuming string length is 4
    static String swapEven(String in) {

        char data[] = {in.charAt(2), in.charAt(1), in.charAt(0), in.charAt(3)};
        String out = new String(data);

        return out;
    }

    // swap a character at odd-numbered index
    // assuming string length is 4
    static String swapOdd(String in) {

        char data[] = {in.charAt(0), in.charAt(3), in.charAt(2), in.charAt(1)};
        String out = new String(data);

        return out;
    }
}

- CJ September 15, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#!/usr/bin/env python
import collections

class Soln(object):
def encode(self, s):
print s

mapodd = [0]*26
mapeven = [0] * 26
encodestr = ""

for i in range(len(s)):
c = s[i]
if i&1:
mapodd[ord(c) - ord('a')] += 1
else:
mapeven[ord(c) - ord('a')] += 1

for i in range(26):
encodestr += str(mapeven[i])
encodestr += str(ord('-'))
encodestr += str(mapodd[i])
encodestr += str(ord('-'))

print encodestr
return encodestr

def distictWords(self, words):
lw = len(words)
if lw == 0:
return 0
ordset = collections.defaultdict(int)
for i in range(lw):
encodestr = self.encode(words[i])
ordset[encodestr] += 1

print ordset
return len(ordset)

soln = Soln();
print soln.distictWords(["abcd", "cbad", "dbac"])
print soln.distictWords(["abcd", "cbad"])
print soln.distictWords(["abcd", "acbd", "adcb", "cdba", "bcda", "badc"])

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

For each string, sort all even characters among themselves. For each string, sort all odd characters among themselves. Then start putting the strings in a hashmap. For example, if String s1 after sorting matches string s2, s1 is key and s2 is part of a list which is value of s1. Repeat for any strings which do not match.

- Maverick October 14, 2018 | 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