Microsoft Interview Question for Software Engineer in Tests






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

Mark all nodes "unexplored" at first. Then use depth first search, mark the nodes we visited as "explored". Whenever we meet a node which has already been marked as "explored", a cycle is detected.

- tangqiyuan May 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It is possible to visit an already visited node in a graph that doesn't have cycles (think about what happens when it finds a cross or forward edge).

A graph has a cycle if a back edge is found during DFS.

- ha2010 May 10, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Do mark the nodes, as "currently being explored" or "not currently explored". When we see an edge to a "currently being explored" node (i.e to a node currently in call stack), it is a back-edge and hence has a cycle.

- Harish May 13, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You need to un-mark a vertex after recursion so no false cycles are reported. For instance if your graph looks like this:

a -> b -> c
       -> d /

If you don't un-mark vertex C after recursion ends there from b, then it will report c involved in a cycle which is incorrect. However if your graph looks like this:

a->b->c->d->a

Then clearly from d you will visit a and find the cycle !

- Ashish Kaila March 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

By using the disjoint set data structure, an efficient way of detecting cycles is possible : en.wikipedia.org/wiki/Disjoint-set_data_structure

Union-by-rank and path compression improves the efficiency yielding an amortized running-time of O(logn) per MakeSet, Union, or Find operation

- mdk May 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

BFS is used to find the cycles in graphs

- erappy July 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Erappy

BFS is normally used for undirected graph not directed!!!!

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

You need to un-mark a vertex after recursion so no false cycles are reported. For instance if your graph looks like this:

a -> b -> c
       -> d /

If you don't un-mark vertex C after recursion ends there from b, then it will report c involved in a cycle which is incorrect. However if your graph looks like this:

a->b->c->d->a

Then clearly from d you will visit a and find the cycle !

- Ashish Kaila March 10, 2011 | 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