Microsoft Interview Question


Country: United States
Interview Type: In-Person




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

Assuming it is a finite graph, use dfs traversal using colours. To speed things up you may use multiple threads and colours to detect cycles. If the graph is disjoint search must start from a node of each disjoint part.

- Noobie January 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes. It seems very simple.
1. Represent nodes in adjecency list. Keep a bool visited[total_nodes] array. Initially all values set to false.
2. Write a dfs function for a given node.
signature: bool Print_cycle(int node_number)
2. call above Print_cycle in a function say Make_work_Disjoint. In this iterate on visited[] and call Print_cycle for every node which is not visited.

- Sugarcane_farmer May 18, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use a simple DFS. starting from one of the disjoint parts and using a "visited" flag for each node (can be stored in an array, each for a disjoint part) and when a back edge is found, ie when a node which was already visited is found, we have our loop.

- dummy January 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Very straightforward

class LoopFinder
    {
        public List<Node> FindLoop(Node node)
        {
            if (node == null)
                throw new ArgumentNullException();

            return this.FindLoop(node, new List<Node>());
        }

        private List<Node> FindLoop(Node node, List<Node> visitedNodes)
        {
            if (visitedNodes.Contains(node))
                return new List<Node>(visitedNodes) { node };

            if (node.NextNodes == null || node.NextNodes.Count == 0)
                return null;

            foreach (Node n in node.NextNodes)
            {
                var r = FindLoop(n, new List<Node>(visitedNodes) { node });
                if (r != null)
                    return r;
            }

            return null;
        }
    }

- vee January 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is just what came in my mind. I have not tested this code. It may contain some errors.

//N is number of nodes.
void findLoop(int matrix[][]){
	int arr[N];
	int count=0;
	int i=0,j=0;
	int visited[N]={0};
	while(i< N && j < N){
		if(visited[i] == 1){
			break;
		}
		if(arr[i][j] != 0){
			i = j;
			j = 0;
			arr[count++] = i;
			visited[i] = 1;
		}else{
			j++;
		}
	}
	j=0;
	while(j<count){
		if(arr[j] == i){
			break;
		}
		j++;
	}
	
	while(j<count)
		printf(arr[j])
	
}

- Anonymous January 08, 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