Yatra.com Interview Question Developer Program Engineers

  • yatracom-interview-questions
    0
    of 0 votes
    14
    Answers

    travel the tree vertically like
    2
    3 4
    5 6 7 8

    output:5 3 2 6 7 4 8

    - arun on July 03, 2012 in India Report Duplicate | Flag
    Yatra.com Developer Program Engineer Algorithm

Country: India
Interview Type: In-Person


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

What is the question? Please elaborate

- loveCoding on July 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

public void traverse_vertical(Tree tree)
    {
    	if(tree == null)
    		return;
    	
    	stack.push(tree.element);
    	traverse_vertical(tree.left);
    	while(stack.size()!=0)
    	{
    		System.out.print(stack.pop());
    	}
    	traverse_vertical(tree.right);
    	
    }

reply me if i am wrong

- cobra on July 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Is the tree structure -

2
/ \
3 4
/ \ / \
5 6 7 8

- Amruta on July 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

2
                          /       \
                        3            4
                    /        \       /       \
                 5           6     7       8



output:5 3 2 6 7 4 8
(note: 2,6,7 are in the same line)

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

queue s1;
s1.push(root);
while(!s1.empty())
{
  node q=s1.front();
 if(q is a leaf) print q; pop s1;
else if(left child of q explored && q->right!=NULL)
{ 
     print q; push(q->right);
     pop s1;
}
else if( q->left is explored && !q->right)
{
 print q; pop s1;
}
else
 push(q->left);
}

- codez on July 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do inorder Tree traversal, at each instance push root into stack.
When root becomes null print stack.

tree_traverse(node* root)
{

if(root == null && stack!= empty)
{
print stack
return;
}
s.push(root.value);

tree_traverse(root->left);
tree_traverse(root->right);
}

- nerd on July 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Won't work. Lets say you have pushed 2,3 &5.
Now the root becomes NULL, print it & return. However you are not emptying the stack.So, 2,3 remains in stack & again you push 6 to stack. The root becomes NULL in next call. You print 6,3,2.

In simple, you have written the program to print all leaf to root paths.

- Aashish on July 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

When you print stack you have to pop all the elements to get the element. Do you have an implementation of stack which allows you to traverse it without popping? :) It becomes List if you can traverse.

- nerd on July 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Print Stack here means print all the value in the stack.
That means print till the stack becomes empty.

- nerd on July 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is the function:

void printV(struct node *root,int *arr,int *depth)
{
        if(root)
        {
                arr[++(*depth)]=root->data;
                printV(root->left,arr,depth);
                
                if(!(root->left || root->right))
                {
                        while((*depth)>=0)
                                printf("%d ",arr[(*depth)--]);
                }
                
                printV(root->right,arr,depth);
        }
}

Complete code: ideone.com/IAKcD

- Aashish on July 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this code work for only symmetric tree ....if the tree is like .
for asymmetric it will not work

2
                                        /       \
                                    3             4
                                                /
                                             5
                                          /
                                      6
                                   /
                                 7
                               /
                             8

output: 8 7 3 6 2 5 4

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

correct me where i am wrong .........i am trying to do is in O(n).....but is in O(nlgn)

int breath_left(struct node* Root)
{
	if(!Root)
		return 0;
	return max(breath_left(Root->left)+1,breath_left(Root->right)-1);
}
int breath_right(struct node* Root)
{
	if(!Root)
		return 0;
	return max(breath_right(Root->right)+1,breath_right(Root->left)-1);
}
int breath(struct node* Root)
{
	return (breath_left(root)+breath_right(root)-1);
}
void verticalAtLevel(struct node* Root,int level)
{
	if(!Root)
		return;
	if(level==1)
		printf("%d  ",Root->data);
	verticalAtLevel(Root->left,level-1);
	verticalAtLevel(Root->right,level+1);
	
	
}
void vertical_travers(struct node* Root)
{
	int breathLeft=breath_left(Root);
	int i=0;
	for(i=breathLeft;i>=(-breath_right(Root));i--){
		verticalAtLevel(Root,i);
		printf("\n");}
		
}

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

can some one pls elaborate the question.. i cant understand!!

- Arulmozhi on July 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Pre-order tree traversal

public class Node {

private Node right;
private Node left;
private int nodeValue;

public Node (  )
{
// a Java constructor
}

public Node leftNode() {return left;}
public Node rightNode() {return right;}
public int getNodeValue() {return nodeValue;}

}

void preOrder (Node root)
{

  if(root == null) return;
  
  root.printNodeValue();
  
  preOrder( root.leftNode() );
  preOrder( root.rightNode() ); 
  
}

- Kesha on May 21, 2013 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book walking you through every aspect of getting a job at a top tech company, while focuses on software engineering interviews.

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