Facebook | Phone | Two Pointer Question + Valid Parentheses

Anonymous User
2746
Nov 02, 2021
Nov 03, 2021

Hey everyone,

Just recently finished up my fb E3 phone interview and I got the following questions.

  1. Given vectors SV1 = [0,1,0,0,0,1,0,2,0] and SV2 = [0,2,0,1,0,0,1,1,0,1], the dot product of the two is equal to 4. Now given a compressed version of the vectors (CV1 = [(1,1), (5,1), (7,2)] and CV2 = [(1,2), (4,1), (6,1), (7,1), (9,1)], find the dot product with them. This was pretty simple. Just a two pointer method, if the first pair within the tuple are equal to each other, then you would do the multiplication and add it to a resulting sum. If one number was less than, then you would increment the less than number to the next pair and check.
public int dotproduct(List<List<Integer>> CV1, List<List<Integer>> CV2) {
	int sum = 0;
	int p1 = 0; int p2 = 0;
	if (CV1 == null || CV2 == null || CV1.size() == 0 || CV2.size() == 0) return 0;
	while (p1 < CV1.size() && p2 < CV2.size()) {
		List<Integer> curr1 = CV1.get(p1);
		List<Integer> curr2 = CV2.get(p2);
		if (curr1.get(0) == curr2.get(0)) {
			sum += curr1.get(1) * curr2.get(1);
			p1++;
			p2++;
			} else {
				if (curr1.get(0) > curr2.get(0)) p2++;
				else p1++;
			}
		}
	}
	return sum;
	}
  1. An expression can use the following: [a-z], [A-Z], [0-9], (), <>, {}, [], check to see if it is a valid expression. For instance, 'g(k(x[3]))' returns true, and 'k<f(>2)' returns false. This was just valid parentheses.

(moving to onsite!)

Comments (8)