Facebook Interview Question for Software Developers


Team: Infrastructure
Country: United States
Interview Type: In-Person




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

If we can assume any data structure for vectors, I think, I will you HashMap to store index and corresponding value only for non zero entry.
for example, consider a vector 1 0 0 14 0 0 23 then my map would be
map = [0:1, 3:14, 6:23]
so now all I need to code it up.

Pseudo Code:-

mapA = HashMap for first vector
mapB = HashMap for second vector

sum = 0;  //storing dot product
for each (i:mapA.keys());
	if(mapB.containsKey(i))
		sum += mapA.get(i) * mapB.get(j);

return sum;

- sagartiwari230 July 05, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

@funk, if this would be the case then I think, I will store every position and value in HashMap for both vector and while creation of first mapA, I will you HashSet to store position with non zero value.
Now all I need to iterate through each element in HashSet and check whether it is present in mapB or not, if present multiply their value (take out from mapA and mapB) and add it to sum and finally return sum.

Hope that this help. Happy Learning!!

- sagartiwari230 July 06, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@sagartiwari230 I like your solution a lot, which isn't among the ones that I proposed to the interviewer, however I thought you might like to know that the interviewer asked me insistently to consider a representation of the vectors where for each element you store it's position and it's value like this:

class VectorElement{ int pos; int value; }

and then to represent each vector as an array of these elements and optimize the dot product accordingly.

- funk July 05, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@funk
reading your posts, a solution the interviewer was trying to push you for, was to store pairs of (index, value) in a vector sorted by index, so A = [15, 0, 0, 0, 2, 3] would become [(0, 15), (4,2), (5, 3)]
if you have two of them, the dot product is something like a merge of two sorted lists (A, B are such vectors of pairs sorted by index and R is the result with the same properties)

a = 0
b = 0
R = []
while a < len(A) and b < len(B):
	if A[a][0] > B[b][0]:
		b += 1
	elif A[a][0] < B[b][0]:
		a += 1
	else R.append((A[a][0], A[a][1]*B[b][1]))

It has the same O-properties as your solution but I'd expect it to runs faster in practice because the constants involved in the O(n) are smaler (less work to lookup a hash, sequential memory read (cache))...

- Chris July 06, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@sagartiwari230, yea I thought of that too, but interviewer told me I was not supposed to mess with the construction phase of the vectors because it wouldn't be reusable.

What she wanted me to arrive to (which I didn't at the time) was to use the representation that @ChrisK mentioned and then to use binary search to find the next element on each vector to multiply with each other.

- funk July 06, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here's the solution using binary search

/**
 * a[i][0]: i-element's position in the vector
 * a[i][1]: i-element's value
 */
public static int dotProduct(int[][] a, int[][] b) {
    int result = 0, i = 0, j = 0;

    while ( i < a.length && j < b.length ) {
        if ( a[i][0] == b[j][0] ) {
            result += a[i][1] * b[j][1];
            i++; j++;
        } else if ( a[i][0] > b[j][0] ) {
            j = bsearch(b, a[i][0], j+1, b.length);
        } else {
            i = bsearch(a, b[j][0], i+1, a.length);
        }
    }

    return result;
}

private static int bsearch(int[][] v, int pos, int from, int to) {
    if ( to-from <= 1 ) { return from; }
    int mid = (to-from)/2 + from;
    if ( v[mid][0] == pos ) { return mid; }
    else if ( v[mid][0] > pos ) { return bsearch(v, pos, from, mid); }
    else { return bsearch(v, pos, mid+1, to); }
}

- funk July 11, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you tell time complexity of this code ?

- xyz May 14, 2020 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@chrisk - would the indexes participate in the dot product, or is it first number for array 1 dot first number is array 2 regardless of their relative location?

- nomadicdude July 19, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

<?php

$a=array(4,1,2,1,1,2);
$b=array(1,2,3);

$sum = 0;
foreach($a as $k=>$v){
    if(array_key_exists($k,$b)) $sum=$sum + $v + $b[$k];
}

//retrun $sum
?>

- Mathboy July 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