Amazon Interview Question


Country: India




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

int zigzag(BSTNode tree, int max)
{
int countl=0;countr=0;
//checking left zigzag
while(1)
{
if(tree.left()!=NULL)
{
tree=tree.left();
countl++;
}
else
break;
if(tree.right()!=NULL)
{
tree=tree.right();
countl++;
}
else
break;
}
if(countl>max)
max=countl;

//now check for right zizzag
while(1)
{
if(tree.right()!=NULL)
{
tree=tree.right();
countr++;
}
else
break;
if(tree.left()!=NULL)
{
tree=tree.left();
countr++;
}
else
break;
}
if(countr>max)
max=countr;
//recursively check for all nodes
max=zigzag(tree.left(),max);
max=zigzag(tree.right(),max);
return max;
}

- Anonymous December 19, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Longest Zig Zag path = Diameter of tree
There are two ways to solve this
1.)Calculate DFS to furthest node. From furthest node, recompute DFS for longest path
2.)
ll = Calculate diameter of left sub tree
lr = Calculate diameter of right subtree
ld = Calculate Height of left + right sub tree + 1
calculate max of above quantities in a recursive manner

- quickpuzzlesolver December 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Though I am not clear about what exactly is a zigzag path, I don't think the longest zigzag path is same as the diameter as the diameter of a tree is the number of nodes on the longest path between two leaves in the tree whereas the longest zigzag path according to me has to start from a node and go down to a leaf or maybe stop even before reaching a leaf.

- Vipul December 25, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

From all path from root to leaf
>>>Check if it is zigzag

- Anonymous December 19, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Not necessary longest zizzag path is from root

- Ayush December 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

There are only two zigzag paths from a node.
Travese both and compare with max. Return Max.
Recursively check for all nodes.

- Ayush December 19, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can anybody illustrate with example..Wat do one mean by Zig Zag Path

- Ankur December 19, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Let L denote a left traversal and R denote a right traversal.
U need to find out the longest LRLRLRLR...
I think having given this explanation the solution is easy.

- Doctor.Sai December 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

i think longest zigzag path would that path in binary tree.. = longest zigzag path in left subtree from root to leaf + longest zigzag path in right subtree from root to leaf.

which would be something like that =LRLRLRLRLR +RLRLRLRLRL
can any one tell me .. that this would be also zigzag path in tree like LLLRRLRLR
or LLLLLLLRRRRLLL or not.

- time December 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

if you have like this LLLRRLRLR (longest zig zag path is LRLR)
or LLLLLLLRRRRLLL (LR or RL) thats it.

- Doctor.sai January 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

DoctorSai: consult some doctor.. you are mad...

- swathi December 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int zig_zag(Node n, char dir)
{
     if (n==NULL)
          return 0;
     else if(dir=='l')
          return 1 + zig_zag(n->left, 'r');
     else
          return 1 + zig_zag(n->right, 'l');
}

void max_zig_zag(Node *root)
{
     int l, r;
     l = zig_zag(root->left, 'r');
     r = zig_zag(root->right, 'l');
     if(l>r)
          cout<<l+1<<endl;
     else
          cout<<r+1<<endl;
}

- null-pointer December 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

will it take care of the zig zag in the middle of the tree.

LR-RLRLRLRLRLRLRLRLRL
|
for example here it breaks but RLRLRLRLRLRLRLRLRL is the longest zigzag

- nmc December 29, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

//Here max is the longest zigzagpath length


static Integer longestZigZagPath(BinaryNode root,String type,Integer length){
		
		
		if(type == null){
			longestZigZagPath(root.right, "L", 1);
			longestZigZagPath(root.left, "R", 1);
		}
		if("R".equalsIgnoreCase(type)){
			if(root.right != null)
				longestZigZagPath(root.right, "L", length+1);
			else if(max < length) 
				max = length;
			if(root.left != null)
				longestZigZagPath(root.left, "R", 1);
		}
		if("L".equalsIgnoreCase(type)){
			if(root.left != null)
				longestZigZagPath(root.left, "R", length+1);
			else if(max < length) 
				max = length;
			if(root.right != null)
				longestZigZagPath(root.right, "L", 1);
		}
		
		return length;
	}

- nmc January 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

call like

max = new Integer(0);
longestZigZagPath(root, null, 1);
System.out.println(max);

- nmc January 01, 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