## Facebook Interview Question

Software Developers**Team:**Infrastructure

**Country:**United States

**Interview Type:**In-Person

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

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

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

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); }
}
```

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

- sagartiwari230 July 05, 2017