Google Interview Question


Country: United States




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

if the relation is transitive(from aRb and bRc follows aRc) and symetric (from aRb follows bRa), then it's finding the connected component's in a undirected graph and just pick one vertex from each component.
I assume the relation is not to be treated as transitive, so, I don't see much else then a brute force, maximizing the recursion where you exclude directly related elements from the set of potential candidates. Every element has two options: it is in the set or it isn't. Since it's only about finding the size, I assume we can use some dp-techniques, similar like knapsack.
where those interviews phd level?

- ChrisK November 12, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can try something like this (I assume the relations are not transitive, and are symmetric):
1. Select a node with minimum number of connections, and add it to the output set.
2. Remove the selected node and nodes connected to it from the graph.
3. Go to #1.

- Alex November 12, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

O(n^2) + exp(k) -

public static void main(String[] args) {

		int n = 6;
		int[][] connected = new int[n][n];
		connected[0][3] = 1;
		connected[3][0] = 1;

		connected[1][2] = 1;
		connected[2][1] = 1;

		connected[0][2] = 1;
		connected[2][0] = 1;

		connected[0][4] = 1;
		connected[4][0] = 1;

		int m = max(connected);
		System.out.println(m);
	}

	// S = {a,b,c,d,e,f} and relations {a,d}, {b,c}, {a,c}, {a,e}
	public static int max(int[][] connected) {
		int n = connected.length;
		Map<Integer, List<List<Integer>>> map = new HashMap<Integer, List<List<Integer>>>();
		int[] dp = new int[n];
		dp[0] = 1;

		List<List<Integer>> o = new ArrayList<List<Integer>>();
		List<Integer> lo = new ArrayList<Integer>();
		lo.add(0);
		o.add(lo);
		map.put(0, o);

		for (int i = 1; i < n; i++) {
			dp[i] = dp[i - 1];
			List<List<Integer>> olist = new ArrayList<List<Integer>>();
			List<Integer> l = new ArrayList<Integer>();
			l.add(i);
			olist.add(l);
			map.put(i, olist);

			for (int j = i - 1; j >= 0; j--) {
				List<List<Integer>> connections = map.get(j);
				if (null != connections) {
					for (List<Integer> nodes : connections) {
						boolean canconnect = true;
						for (int node : nodes) {
							if (connected[i][node] == 1) {
								canconnect = false;
								break;
							}
						}
						if (canconnect) {
							dp[i] = Math.max(dp[i], nodes.size() + 1);
							List<List<Integer>> nconn = map.get(i);
							List<Integer> list = new ArrayList<Integer>();
							for (int k : nodes) {
								list.add(k);
							}
							list.add(i);
							nconn.add(list);
						}
					}
				}
			}
		}
		return dp[n - 1];
	}

- sudip.innovates November 13, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

this is the independent set problem. though similar to the matching, it is np-hard.

the relations form the edges between vertices. the problem asks the size of the largest subset of the vertices of a graph with the property that no two are connected by an edge. note that the edges can be of an arbitrary size.

- Anonymous November 13, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@ChrisK, i think your idea works and i just elaborate with a 3-step solution as follows:
Step 1. Construct the undirected graph where each vertex is an element from the given set and each edge is a relation from the list.
Step 2. Compute all the connected components of the graph.
Step 3. Count those components of size 1 (i.e. single-vertex components), and this count is actually the answer to the problem.

- cong.doan November 20, 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