Google Interview Question for Developer Program Engineers






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

topological sorting

find indgree = 0 nodes and put them in a queue Q

get a node N each time out of the Q, and process N; decrease the indegree of the nodes point to by 1; if a node N points to now has indegree of 0, then put it in Q

so on and so forth until there is nothing left in Q; if all nodes are processed, then G has no cycle

- syrus January 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice

- Anonymous March 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

+ in case of you will find a node that is marked with value<0, detect cycle

- mbriskar June 02, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

a directed graph has a cycle if and only if its Depth First Search reveals a back edge.

- gur January 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

You can do it via modified DFS - but instead of visited nodes set use current path set - whenever recursing into a next node add it to the path and remove it on the way back. If you find next node in current path - it's a cycle.

bool cycleExists(const Graph::Node &start,
                const Graph::Node &currentNode,
                std::set<Graph::Node> &pathNodes) {
    pathNodes.insert(currentNode);

    for(auto it = currentNode.adjucents.begin(); it != currentNode.adjucents.end(); ++it) {
        if(pathNodes.find(*it) == pathNodes.end()) {
            if(cycleExists(start, *it, pathNodes)) return true;
            pathNodes.erase(*it);
        } else {
            return true;
        }
    }
    return false;
}

bool cycleExists(const Graph::Node &start) {
    std::set<Graph::Node> pathNodes;
    return cycleExists(start, start, pathNodes);
}

- Iuri Covalisin October 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

this is what i got from wiki, which is said above...

L ← Empty list that will contain the sorted elements
S ← Set of all nodes with no incoming edges
while S is non-empty do
remove a node n from S
insert n into L
for each node m with an edge e from n to m do
remove edge e from the graph
if m has no other incoming edges then
insert m into S
if graph has edges then
output error message (graph has at least one cycle)
else
output message (proposed topologically sorted order: L)

- Anonymous February 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The cycle detection algorithm for trees can easily be modified to work for graphs. The key is that in a
DFS of an acyclic graph, a node whose descendants have all been visited can be seen again without implying
a cycle. However, if a node is seen a second time before all of its descendants have been visited, then there
must be a cycle. Can you see why this is? Suppose there is a cycle containing node A. Then this means
that A must be reachable from one of its descendants. So when the DFS is visiting that descendant, it will
see A again, before it has finished visiting all of A’s descendants. So there is a cycle.
In order to detect cycles, we use a modified depth first search called a colored DFS. All nodes are initially
marked white. When a node is encountered, it is marked grey, and when its descendants are completely
visited, it is marked black. If a grey node is ever encountered, then there is a cycle.

- runnig March 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

can anyone explain the source code of dfs???

- bharti July 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

By doing a BFS or DFS and keeping track of the visited nodes we can detect a cycle. A cycle is detected when we visit an already visited node during the traversal.

- UB_Green January 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

keeping track of only visited will not work. lets say that you have levels in graph.

A-- ----B
|
C
so A & B directs to C, so only visited will say that there is a cycle but its not. We can keep track of incoming and outgoing time stamp and check if incoming TS already exists and is < current TS. it should be simple to do this while doing DFS/BFS

- annom January 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I didn't get u what is the problem with BFS . Graph will be given at a time . What does Timestamp has to offer in this ..!!

- scofield January 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

What is the problem with normal recursive DFS?? I think it is sufficient.

- squall January 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

For DFS, if the graph is huge, and the edge to form the cycle is really deep. you might run out of stack.

Also for BFS, you need to store at least one layer of nodes, which could be exponential in the worst case. If so, you have the risk to run out of memory.

- Andy January 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Is a theoretical exercise so there are no memory problems.
Both algorithms are valid.

- Chema January 26, 2011 | Flag


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