Microsoft Interview Question


Country: United States




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

traverse the whole tree

- Anonymous July 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Yes, if it is not a binary tree then better consider it as a generic graph and then to find the path of a given node from source node perform depth first or breadth first search. You can understand these concepts here
DFS
youtube.com/watch?v=gCNsAKkUHPM
BFS
youtube.com/watch?v=5pNIul92cj0

- Anonymous July 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I have a very basic doubt. If we are using BFS then how will we keep track of path till the parent of the first element of the queue?wont it be easier with the help of recursion in DFS?if anyone could help..

- Anonymous July 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

In BFS algo we maintain parent info for every node except the root(one from which BFS call is being started). Suppose v is in adjancey of u then we maintain a parent[v] = u, sort of info. So to generate the path you can write a recursive function which uses this info and print the path.

void printPath(node v, node s){
if(v == s){
  printf("%s",s);
  return;
}
printPath(parent(v),s); 
   printf("%s",parent(v));
}

- akki August 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Breadth first search.

- Anonymous July 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

BFS is fissible ... because if node is right most last node than time complexity of both BFS DFS algos will be same ..... but if node is in center than BFS will be best ...

- TTP July 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

struct node
{
	int data;
	struct node *child;
	stuct node *sibling;
};


void printLeavePaths(node *root)
{
	if(root == NULL)
		return;
	enQueue(root);
	if(root->child == NULL)
	{
		printQueueElemts(); // With out removing the elements from the 				    //	queue
	 	deQueue();
		if(root->sibling != NULL)
		{
			printLeavePaths(root->sibling);
			return;
		}
	}

	if(root->child != NULL)
	{
		printLeavePaths(root->child);
		if (root->sibling == NULL)
		{
			// Delete non path elemts for the new path
			deQueue();
			return;
		}
	}

	if(root->sibling != NULL)
	{
		// Delete recently queued elem	
		deQueue()
		printLeavePaths(root->sibling);
	}
}

- Tendulkar September 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Just need to modify printQueueElemts function to check for the path we needed ..

- Tendulkar September 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

consider it as Graph, do BFS or DFS..

- cobra July 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Cobra +1
all trees are Graphs. You can use BFS or DFS to traverse a graph.

- Arulmozhi July 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

DFS will be sufficient. No advantage of using BFS and maintaining queue.

Also, InOrder or PreOrder or PostOrder traversal will help to find node as it is binary tree and not a BST.

- Sach July 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@sachin: for DFS, you will use stack, for BFS you will use queue..
what is the advantage of DFS over BFS?

- cobra July 14, 2012 | 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