## 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;``````

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!!

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.

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] > B[b]:
b += 1
elif A[a] < B[b]:
a += 1
else R.append((A[a], A[a]*B[b]))``````

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))...

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.

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

Here's the solution using binary search

``````/**
* a[i]: i-element's position in the vector
* a[i]: 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] == b[j] ) {
result += a[i] * b[j];
i++; j++;
} else if ( a[i] > b[j] ) {
j = bsearch(b, a[i], j+1, b.length);
} else {
i = bsearch(a, b[j], 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] == pos ) { return mid; }
else if ( v[mid] > pos ) { return bsearch(v, pos, from, mid); }
else { return bsearch(v, pos, mid+1, to); }
}``````

Comment hidden because of low score. Click to expand.
0

Can you tell time complexity of this code ?

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?

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
?>``````

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.

### 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.