Amazon Interview Question for Software Engineer / Developers


Country: India




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

private static void zigZagTraversal(BinaryTreeNode root) {
		int leftToRight=1;
		BinaryTreeNode temp;
		Stack<BinaryTreeNode> currentLevel=new Stack<BinaryTreeNode>();
		Stack<BinaryTreeNode> nextLevel=new Stack<BinaryTreeNode>();
		
		System.out.println("Zig Zag Traversal");
		currentLevel.push(root);
		while (!currentLevel.isEmpty()) {
			temp = currentLevel.pop();
			if(temp!=null)
			{
				System.out.println(temp.getData());
				if(leftToRight==1)
				{
					if (temp.getLeft() != null)
						nextLevel.push(temp.getLeft());
					if (temp.getRight() != null)
						nextLevel.push(temp.getRight());
				}
				else
				{
					if (temp.getRight() != null)
						nextLevel.push(temp.getRight());
					if (temp.getLeft() != null)
						nextLevel.push(temp.getLeft());
				}
				
			}
			if(currentLevel.isEmpty())
			{
				leftToRight=1-leftToRight;
				while(!nextLevel.isEmpty())
				{
					currentLevel.push(nextLevel.pop());
				}
			}
			
			
		}
	}

- Vir Pratap Uttam May 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

public List<Node> getOrderedNodes(Node rootNode) {
        
        List<Node> result = new LinkedList<Node>();
        
        Stack<Node> currentLevel = new Stack<Node>();
        Stack<Node> nextLevel = new Stack<Node>();
        currentLevel.push(rootNode);
        
        boolean isLeftToRight = true;
        
        while( !currentLevel.isEmpty() ){
            Node currentNode = currentLevel.pop();
            
            if( currentNode != null ){
                result.add(currentNode);
                
                if(isLeftToRight){
                    nextLevel.push(currentNode.getLeft());
                    nextLevel.push(currentNode.getRight());
                }else{
                    nextLevel.push(currentNode.getRight());
                    nextLevel.push(currentNode.getLeft());
                }
            }
            
            if(currentLevel.isEmpty()){
                isLeftToRight = !isLeftToRight;
                Stack<Node> temp = currentLevel;
                currentLevel = nextLevel;
                nextLevel = temp;
            }           
        }
        
        return result;
    }

- hakanserce January 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can you please provide an example to explain the question?

- hero January 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You can find this question on Leetcode online judgement,
The question is "Binary Tree Zigzag Level Order Traversal"

- Beserk January 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you please add an example here....

- DashDash January 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Suppose the tree is :
5
/ \
3 7
/ \ / \
1 4 6 8

Then the algo should return : 5->7->3->1->4->6->8

- popoff January 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do level order traversal inserting nodes in an array with every odd level elements inserted in reverse order.

- dashdash January 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

just like a normal level order traversal..maintain a queue. Use some extra variable to track, how elements are to be printed viz. for even level they be printed in the same sequence they are present in queue and for odd level they are printed in reverse order.

void func(struct node *ptr)
{
     if(ptr==NULL)
     return;
     int n;
     n=numNodes(ptr);
     struct node *arr[n]={NULL}; //n is the number of nodes in tree..initialize array to NULL
     int cl,nl,level,count,index,p,q,i;
     arr[0]=ptr;
     nl=count=index=0;
     p=q=0;
     cl=1;
     while(arr[index]!=NULL)
     {
                            if(level==0)
                            printf("%d",arr[0]->num);
                            cl--;
                            if(arr[index]->left!=NULL)
                            {
                                                   arr[++count]=ptr->left;
                                                   nl++;
                            }
                            if(arr[index]->left!=NULL)
                            {
                                                   arr[++count]=ptr->right;
                                                   nl++;
                            }
                            if(cl==0)
                            {
                                     p=q;
                                     q=index;
                                     cl=nl;
                                     nl=0;
                                     if(!(level%2))   //even level
                                     {
                                                      i=p+1;
                                                      while(i<=q)
                                                      {
                                                                 printf("%d",arr[i]->num);
                                                                 i++;
                                                      }
                                     }
                                     else             //odd level
                                     {
                                                      i=q;
                                                      while(i>p)
                                                      {
                                                                printf("%d",arr[i]->num);
                                                                 i--;
                                                      }
                                     }
                                     level++;
                            }
                            index++;
     }
}

- k2 January 03, 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