Yatra.com Interview Question for Developer Program Engineers


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 July 04, 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 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 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 July 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

2,6,7 are on the same vertical axis. Then I is 6,2,7 equivalent to 2,6,7 ? If they are then this is a inorder traversal.

- Shivam May 08, 2014 | 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 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 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 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 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 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 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 July 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 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 July 05, 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 July 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It basically wants you to travverse nodes of the tree in vertical fashion. Like draw the tree make sets of nodes drawing lines in vertical fashion paralley. Basically you need to take reference of root let it be at zero coordinate. If you go from this to its left child coordinate will become -1 (that is keep noting coordinate of current node while traversing the tree go to its left child do (coordinate of current node -1) while going to right child do cooridinate of current node +1. keep storing list of nodes with same coordinate then print elemnts of lists startting from least to maximum coordinate)

- Poonam March 09, 2014 | Flag
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 May 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int[] input = {2, 3, 4, 5, 6, 7, 8};
		boolean[] visited = new boolean[input.length];
		
		int start = input.length / 2;
		for (int i = start; i <= input.length - 1; i++) {
			System.out.println(input[i]);
			visited[i] = true;
			int j = i;
			while (j > 0) {
				j = (j-1) / 2;
				if (!visited[j]) {
					System.out.println(input[j]);
					visited[j] = true;
				}
			}
		}

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

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.Stack;

import javax.xml.stream.events.NotationDeclaration;

/*
 * 			1
 * 		2		3
 * 	4		6		7 
 * 
 * 
 */
public class VerticalTraversal {
	static class Node {
		Node left;
		Node right;
		public int element;
	}

	static Map<Integer, List<Integer>> map = new HashMap<>();
	public static void traverseAndUpdateMap(Node tree, int verticalIndex) {
		if(tree == null){
			return;
		}
		
		List<Integer> value = map.get(verticalIndex);
		if(value == null)
			value = new ArrayList<>();
		value.add(tree.element);
		map.put(verticalIndex, value);
		
		traverseAndUpdateMap(tree.left, verticalIndex-1);
		traverseAndUpdateMap(tree.right,verticalIndex+1);
	}
	
	public static void printVerticleTraversal(Node tree) {
		traverseAndUpdateMap(tree, 0);
		List<Integer> keys = new ArrayList<>(map.keySet());
		keys.sort(Comparator.naturalOrder());
		for(int i : keys){
			List<Integer> list = map.get(i);
			for(int j : list){
				System.out.print(j + " ");
			}
		}
	}
	public static void main(String[] args) {
		Node tree = new Node();
		tree.left = new Node();
		tree.right = new Node();
		tree.left.right = new Node();
		tree.left.right.left = new Node();
		tree.left.right.left.left = new Node();
		// tree.left.right = new Node();
		// tree.right.left = new Node();
		tree.right.right = new Node();

		tree.element = 1;
		tree.left.element = 2;
		tree.right.element = 3;
		tree.left.right.element = 4;
		tree.left.right.left.element = 5;
		tree.left.right.left.left.element = 6;
		// tree.left.right.element = 5;
		// tree.right.left.element = 6;
		tree.right.right.element = 7;

		printVerticleTraversal(tree);
	}
}

This code will work for every scenario, correct me if I am wrong

- deepak September 17, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I'm using the following approach and it is giving result:
1. read recursively till root.left is not null
2. print node data and if it has a right node then add it in a list
3. now read through the list
4. if it has no left right nodes then print data else call traverse method on this node again.

Please comment if this solution looks ok..

List<Node> queue = new ArrayList<Node>();

public void traverse(Node root)
{
if(root.left != null)
traverse(root.left);
System.out.print(root.data+" ");
if(root.right != null)
queue.add(root.right);
}

public void printqueue()
{
if(queue.size() == 0)
return;
if(queue.size() > 0)
{
Node t = queue.get(0);
if(t.left == null && t.right == null)

{ System.out.print(t.data);
queue.remove(0);
}
else {
traverse(t);
queue.remove(0);
}

printqueue();
}
}

public static void main(String args[])
{
Node n1 = new Node(2);
Node n2 = new Node(3);
Node n3 = new Node(4);
Node n4 = new Node(5);
Node n5 = new Node(7);
Node n6 = new Node(8);

Node n7 = new Node(6);

n1.left = n2;
n2.left = n4;
n2.right = n7;
n3.left = n5;
n3.right = n6;
n1.right = n3;
VerticalTree obj = new VerticalTree();
obj.traverse(n1);
obj.printqueue();
}

- devashish2008 March 26, 2017 | 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