Facebook Interview Question for Software Engineer Interns


Country: United States
Interview Type: Phone Interview




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

This is how you do it :

si = set(1,2,3)
sf = set(1.0,2.0,3.0)
sb = set( true, false )
// generate cross product? very well :
p = si * sf * sb 
// not satisfied ? 
p = join ( si, sf, sb ) 
// want to load from array ?
p = join ( @ARGS = [ si, sf, sb ] )
// so, you wan to seriously code? Not cool.

In that case, here is how ZoomBA implemented join operation :-) from ZoomBA with love:

// full iterative version, w/o any stack or recursion, should be good to watch... 
static boolean next(Object[] tuple, List<Iterator> states, Iterable[] cc) {
        boolean carry = true;
        for (int i = cc.length - 1; i >= 0; i--) {
            if (!carry) break;
            Iterator iterator = states.get(i);
            if (iterator.hasNext()) {
                tuple[i] = iterator.next();
                carry = false;
            } else {
                if (i == 0) return true;
                carry = true;
                iterator = cc[i].iterator();
                tuple[i] = iterator.next();
                states.set(i, iterator);
            }
        }
        return false;
    }

public static Collection join(Collection into, Function predicate, Function map, Iterable[] cc) {
        if (cc.length < 2) return into;
        int index = 0;
        Object[] tuple = new Object[cc.length];
        List<Iterator> myStates = new ArrayList<>();
        for (int i = 0; i < cc.length; i++) {
            Iterator iterator = cc[i].iterator();
            if (!iterator.hasNext()) return into;
            tuple[i] = iterator.next();
            myStates.add(iterator);
        }
        boolean carry = false;
        while (!carry) {
            Function.MonadicContainer result = predicate.execute(index, tuple, cc, into);
            if (!result.isNil() && ZTypes.bool(result.value(), false)) {
                result = map.execute(index, tuple, cc, into);
                extractResult( into, result, tuple );
            }
            if (result instanceof Break) {
                extractResult( into, result, tuple );
                break;
            }
            carry = next(tuple, myStates, cc);
            index++;
        }
        return into;
    }

- NoOne December 22, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In recursive mode, we can do cool in ZoomBA :

def do_join( tuple, arr, result ){
  i = size(tuple)
  if ( i == size(arr) ){
     result.add ( tuple )
     return
  }
  for ( item : arr[i] ){
     do_join ( tuple + item, arr, result )
  }
}

s1 = [0,1,2]
s2 = [true, false]
arr = [s1, s2]

result = list()
do_join( [], arr, result )
println ( result )

- NoOne December 22, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/**
     * Assuming that sets dont have duplicates as in the Mathematical definition of set.
     * @param sets: The input list of the Sets.
     * @return Intersection of the sets.
     */
    public List<Integer> setIntersection(final List<Set<Integer>> sets) {
        final List<Integer> intersection = new ArrayList<>();
        if (sets.isEmpty()) {
            return intersection;
        }

        final Map<Integer, Integer> occurance = new HashMap<>();

        for (final Set<Integer> s : sets) {
            for (final Integer i : s) {
                if (occurance.containsKey(i)) {
                    occurance.put(i, occurance.get(i) + 1);
                } else {
                    occurance.put(i, 1);
                }
            }
        }
        for (final Integer key : occurance.keySet()) {
            if (occurance.get(key).equals(sets.size())) {
                intersection.add(key);
            }
        }

        return intersection;
    }

- artur.ghandilyan December 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think they asked not intersection. Cross products of sets is
A = {a1, a2} B = {b1, b2} C = {c1} =>
{a1, b1, c}, {a1, b2, c} {a2, b1, c} and {a2, b2, c}

- Alex M. February 04, 2017 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

def product(*args):
    prod = [[]]
    for arg in args:
        prod = [x + [y] for x in prod for y in arg]
    
    return prod

a = [[1,2,3], [4,5,6], [7,8,9]]
print product(*a)

- Nitish Garg January 02, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void productOfSets(std::vector<std::set<int>> vec)

{
int mul = 1;
	int smallest = vec[0].size();
	int largest= vec[0].size();
	for (size_t l = 0; l < vec.size(); l++)
	{
		smallest = vec[l].size() < smallest ? vec[l].size() : smallest;
		largest = vec[l].size() > largest ? vec[l].size() : largest;
	}

	for (size_t k = 0; k < smallest; k++)
	{
		mul = 1;
		for (int i = 0; i < vec.size(); i++)
		{
			std::set<int>::iterator itr = vec[i].begin();
			for (int j = 0; j < k; j++)
			{
				itr++;
			}
			mul = *itr * mul;
		}
		std::cout << mul << "\n";
	}
	for (size_t t = 0; t < largest-smallest; t++)
	{
		std::cout << "0" << "\n";
	}

}

- Sneha Patil January 25, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void product(std::vector<std::set<int>> vec)
{
int mul = 1;
	int smallest = vec[0].size();
	int largest= vec[0].size();
	for (size_t l = 0; l < vec.size(); l++)
	{
		smallest = vec[l].size() < smallest ? vec[l].size() : smallest;
		largest = vec[l].size() > largest ? vec[l].size() : largest;
	}

	for (size_t k = 0; k < smallest; k++)
	{
		mul = 1;
		for (int i = 0; i < vec.size(); i++)
		{
			std::set<int>::iterator itr = vec[i].begin();
			for (int j = 0; j < k; j++)
			{
				itr++;
			}
			mul = *itr * mul;
		}
		std::cout << mul << "\n";
	}
	for (size_t t = 0; t < largest-smallest; t++)
	{
		std::cout << "0" << "\n";
	}

}

- Sneha Patil January 25, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a = [[1,2],[3,4],[5,6]]

def crossprod(a,f):
    if a == []:
        print(f)
    else:
        for e in a[0]:
            f.append(e)
            crossprod(a[1:],f)
            del f[-1]
        
    
crossprod(a, [])

- Armin February 01, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a = [[1,2],[3,4],[5,6]]

def crossprod(a,f):
    if a == []:
        print(f)
    else:
        for e in a[0]:
            f.append(e)
            crossprod(a[1:],f)
            del f[-1]
        
    
crossprod(a, [])

- Armin February 01, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hlw

- shoshi March 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