Google Interview Question for Software Engineer Interns


Country: United States
Interview Type: Phone Interview




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

Acyclic undirected graph:

V-1 edges. Spanning tree of the graph.


Acyclic directed graph:

Imagine the graph topologically sorted. It can only have edges going down (from top to bottom), otherwise it would have cycles.

The way to maximize the number of edges without creating cycles is to connect a given vertex to all other vertices that are below it (again, imagining the graph in a topological order). For instance, the vertex 0 is connected to 1, 2, 3, ..., V-1; vertex 1 to 2, 3, ..., V-1. So on and so forth.

Therefore, the total number of edges is: Sum (i=0 to V-1) i, which is equal to (V^2-V)/2.

- Victor November 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 votes

That's a very good analysis but I question your second answer. I feel the total number is unbounded because you can have multiple directed edges from vertex A to vertex B. In your example if this were sorted it would only have one edge. But really it could have 20K edges going from A to B. It really just depends on how they are arranged. Is the question over possibility?..

- Markymark November 23, 2014 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 votes

Thanks! Actually you can't have multiple edges from vertex A to B, otherwise you'd have a multigraph, not a graph.

When it's talked about graphs, very rarely it includes multigraphs. I believe the question would have made it explicit if multigraphs were allowed.

- Victor November 24, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

For directed/undirected acyclic graph: nC2 - 1 (because there is only 1 cyclic graph possible)

- Soumitra November 23, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

(V^2 - V)/2 is the maximum number. Let's prove it.

Let G(V, E) be an acyclic graph with the most number of edges. Then:

Claim: There is a (the) sink node with exactly V - 1 incoming edges where a sink node is a vertex with no outgoing edges.

Proof: First note that we have to have a sink node. Otherwise, any vertex will have at least 1 outgoing edge and hence, a cycle.

Now note that for the optimal graph, the sink node must have "V - 1" incoming edges. Simply if it does not, then you can add the missing edges to the sink node and still produce no cycles. Therefore, the graph could not have possibly been optimal.

Therefore, the sink node has "V - 1" incoming edges.

Now the rest is easy. Remove this sink node from this graph and all the edges connected to it. You will get another G(V - 1, E) which is optimal. By recusing, we can go all the way to the graph with one vertex and 0 edges. This way, we are removing
(V - 1), (V - 2), ..., (1) edges to get to that graph. Therefore, the total number of edges is at most (V^2 - V) / 2.

On the other hand, as suggested above, you can indeed construct a graph with this many edges. Add an edge from vertex "0" to all other vertices except itself. Then from vertex "1" to all vertices except {0, 1}. Iterate until exactly "(V^2 - V) / 2" edges are added.

- Ehsan November 25, 2014 | 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