Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

consider the tree to be the directed graph, perform the regular DFS to find the paths between root and all the nodes and find the max value at the same time. Print the path to the max node in the end.

- S.Abakumoff February 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

here is the C# code:

class Dfs<T> where T : IComparable<T>
{
	private readonly Node<T> _source;
	private readonly Dictionary<T, T> _paths = new Dictionary<T, T>();
	private T _max = default(T);
	public Dfs(Tree<T> tree)
	{
		_source = tree.Root;
	}
	public void Solve()
	{
		Search(_source);
		var stack = new Stack<T>();
		stack.Push(_max);
		for(var n = _paths[_max]; _source.Value.CompareTo(n)!=0;n=_paths[n])
		{
			stack.Push(n);
		}
		stack.Push(_source.Value);
		while(stack.Count>0)
		{
			Console.WriteLine(stack.Pop());
		}
	}
	private void Search(Node<T> node)
	{
		if(node.Value.CompareTo(_max) > 0)
		{
			_max = node.Value;
		}
		if(node.Left!=null)
		{
			_paths.Add(node.Left.Value, node.Value);
			Search(node.Left);
		}
		if(node.Right!=null)
		{
			Search(node.Right);
			_paths.Add(node.Right.Value, node.Value);
		}
	}
}

- S.Abakumoff February 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

firstly it's not bst, so your comparison "_source.Value.CompareTo(n)!=0" is not valid.
secondly you are using O(n) space which can be reduced to O(lgn)

- zyfo2 February 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

_source.Value.CompareTo(n)!=0 it's not about Bst.
Read the code first, comment second.

- S.Abakumoff February 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

traverse once to find the max node first. then dfs to pass down the values in array till reach the max node

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

Recursive solution:
find the index of max value in left and right subtree and return the index of max value among root, left and right.
Once we get the index of max node in the tree, then path will be index/2 recursively..

#define NOT_DEFINED	-1000000007
vector<int> tree(n);
int main(){

	tree[0]=NOT_DEFINED;
	// Input tree vector starting at root node 1
	// left child index= 2*node
	// right child index= 2*node+1
	
	int val = func(1);
	printPath(val);
	return 0;
}

void printPath(int node){
	if(node !=1)
		printPath(node/2);
	printf(node);
}

int max_index(int a, int b){
	if(tree[a]>tree[b]) return a;
	return b;
}

int func(int node){
	if(tree[node] == NOT_DEFINED)
		return 0;
	int left = func(2*node);
	int right = func(2*node+1);
	return max_index(node,max_index(left,right)); 	
}

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

void maxpath(struct Node *node,int *arr,int max_arr[],int index,int &max,int &max_index)
{
if(!node)
return;
arr[index++]=node->data;
if(!node->left && !node->right)
{
int temp=0;
for(int i=0;i<index;i++)
temp+=arr[i];
if(temp>max)
{
for(int i=0;i<index;i++)
max_arr[i]=arr[i];
max=temp;
max_index=index-1;
}
}
if(node->left)
maxpath(node->left,arr,max_arr,index,max,max_index);
if(node->right)
maxpath(node->right,arr,max_arr,index,max,max_index);
}

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

We would find out the location of the node with max value by traversing the tree.After finding it,we would again traverse from root ,and put the entry of the last traversed node in an array ,but each time we enconter the leaf node ,we will put 0 in the array.Along with address ,we also put height of each node.We will update till we reach the max node.After that we need to just traverse the array aand find the height of each node,till we reach the root

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

it is easy to find max value via recursion,
it is a little tricky to pass the path information. The recursive function returns the max sum of the current tree, and uses a reference to return the path generating the sum at the same time.

#include <vector>
#include <iostream>
using namespace std;
void print( vector<int> v){
	vector<int>::iterator it ;
	for( it = v.begin(); it != v.end(); ++it )
		cout << *it << " " ;
}

struct Node{
	Node* 	left;
	Node* 	right;
	int 	value;
};

int maxPath( Node* n, vector<int> &path ){
	if( n != NULL ){
		//cout<<n->value<<endl;
		vector<int> leftPath;
		int leftMax = maxPath( n->left, leftPath );

		vector<int> rightPath;
		int rightMax = maxPath( n->right, rightPath );

		path = leftMax > rightMax ? leftPath : rightPath;
		int ret = leftMax > rightMax ? leftMax : rightMax;

		path.push_back( n->value );
		
		return ret + n->value;
	}
	else
		return 0;
	
}



int main(){
	Node* root = new Node;
	root->value = 1;
	root->left = new Node;
	root->left->value = 2;
	root->right = new Node;
	root->right->value = 3;
	root->right->left = new Node;
	root->right->left->value = 4;

	vector<int> path;
	cout<<maxPath(root,path)<<endl;
	print(path);

}

- anryhuang February 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do a DFS from root, maintaining a stack of max values. Each time you pop a node compare its value to the top of the max stack. If its a match, pop the stack as well. When you hit the target node, the stack top holds the Max value in its path.

- ns2 February 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I was thinking that if we maintain a stack of nodes build by traversing the tree. While building, we follow a specific pattern - we push parent, then parent.left and then parent.right. If any of the parent doesn't have any child, we push zero value in that place.

After constructing such stack, we could easily find out which node has max value. If the max value node is found in odd index, then the path from the root will be the nodes at odd position index till that maximum node. Or otherwise, it will be the even position indexes.

- sam February 18, 2013 | 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