Google Interview Question
1,227 Interview Reviews |
Back to all Google Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Software Engineering Intern at Google:
if we had a list of n nodes, what are the maximum number of edges there can be for a directed acyclic graph?
See more for this Google Software Engineering Intern Interview
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (3)
0 of 3 people found this helpful
Graph with 3 vertexes, has at most 2 edges before creating a cycle (adding third edge would create a triangle of edges hence a loop).
Same with 4 vertex graph, and so on..
Helpful Answer?
Yes |
No
Inappropriate?
3 of 3 people found this helpful
If you look at a graph of size 1, it's 0.
2, it's 1.
3, it's 3 (a -> b, a ->c, b ->c)
4, it's 6 (a -> b, a -> c, a ->d, b->c, b->d, c->d)
If you notice, the first node points to all of the other nodes except itself, the next node points to all the other nodes except the first node and itself, and this keeps decreasing by one, so you get (n-1) + (n-2) + ... + 2 + 1
Knowing that the sum from 0 to n = (n*(n+1))/2, this is the sum of 1 to n-1, which is (n*(n-1))/2.
Helpful Answer?
Yes |
No
Inappropriate?
To comment on this
question,
Sign In with Facebook or
Sign Up
3 of 4 people found this helpful
by Interview Candidate: