Amazon Interview Question for Software Engineer / Developers






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

my solution, extrac data structure : 1 queue 2 stack

struct TreeNode
{
      int    Element;
      TreeNode *Left;
      TreeNode *Right;
      TreeNode *Third;
}  *NodePosition;

void	ZigZag(NodePosition root)
{
	queue<NodePosition> que;
	stack<NodePosition>  stk;
	bool	level = true; // level indicator
	que.push(root);
	que.push(NULL);
	NodePosition prev = NULL;
	NodePosition curr = NULL;

	while(!que.empty())
	{
		//1. Dequeue
		curr = que.front();
		que.pop();

		//2. Reach level end
		if (curr == NULL)
		{
			// put element in stack to queue
			while(!stk.empty())
			{
				que.push(stk.top());
				stk.pop();
			}
			level = level == true ? false : true;  // change level indicator alternately
			if(que.empty())
				break;
			que.push(NULL);
			curr = que.front();
			que.pop();
		}

		//3. Put current node’s children to stack
		if(level)
		{
			if(curr->Left)
				stk.push(curr->Left);
			if(curr->Right)
				stk.push(curr->Right);
		}
		else
		{
			if(curr->Right)
				stk.push(curr->Right);
			if(curr->Left)
				stk.push(curr->Left);
		}

		// 4. Make prev’s third pointer point to current node
		if(prev)
			prev->Third = curr;
		prev = curr;
	}
	prev->Third = NULL;
}

- Anonymous February 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Seems like the easiest to understand code, I am not sure if inplace is feasible for this problem?

- amritaansh123 February 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Modified BFS.
Use a stack to store the nodes of same level (Push in alternate order)
Level 2 : Push right to left
Level 3 : Push left to right
..
Pop the nodes from the stack and enqueue it only when the queue has become empty.

- anon July 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This can be done in place without additional space. Assume level n's pointers are fixed. Using these pointers, we can fix the pointers for level n+1. Explicitly fix the pointers for level 0 alone. Once it is done, apply above mentioned strategy.

- Abee July 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The idea sounds good but implementation seems not easy

- Anonymous July 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

is it a doulbe linked list or single
cause if it is single, how to do that? Any ideas?
i.e. if we have linked 7 to 3, how to get the last row linked?
5
3<-7
2->4->6->9

- Anonymous July 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can we use a queue?
Enqueue the root first,
when dequeue a node, enqueue its left and right nodes.

- Anonymous July 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I want the code

- Anonymous October 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Solution without additional memory.

public class MainClass {

    public static void main(String[] args) {
        TreeItem root = init();

        linkLevel(root, true);

        printTree(root);
    }

    private static void linkLevel(TreeItem startNode, boolean direction) {
        TreeItem currentNode = startNode;

        TreeItem lastSibling = null;
        do {
            if (direction) {
                if (currentNode.left != null) {
                    currentNode.left.sibling = lastSibling;
                    if (currentNode.right != null) {
                        currentNode.right.sibling = currentNode.left;
                        lastSibling = currentNode.right;
                    } else {
                        lastSibling = currentNode.left;
                    }
                } else if (currentNode.right != null) {
                    currentNode.right.sibling = lastSibling;
                    lastSibling = currentNode.right;
                }
            } else {
                if (currentNode.right != null) {
                    currentNode.right.sibling = lastSibling;
                    if (currentNode.left != null) {
                        currentNode.left.sibling = currentNode.right;
                        lastSibling = currentNode.left;
                    } else {
                        lastSibling = currentNode.right;
                    }
                } else if (currentNode.left != null) {
                    currentNode.left.sibling = lastSibling;
                    lastSibling = currentNode.left;
                }
            }
            if (currentNode.sibling == null) {
                currentNode.sibling = lastSibling;
                break;
            } else {
                currentNode = currentNode.sibling;
            }
        } while (true);

        if (lastSibling != null) linkLevel(lastSibling, !direction);
    }

    private static void printTree(TreeItem item) {
        while (item != null) {
            System.out.print(item.value);
            System.out.print(" ");
            item = item.sibling;
        }
    }

    private static TreeItem init() {
        TreeItem A = new TreeItem("A");
        TreeItem B = new TreeItem("B");
        TreeItem C = new TreeItem("C");
        TreeItem D = new TreeItem("D");
        TreeItem E = new TreeItem("E");
        TreeItem F = new TreeItem("F");

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.right = F;
        return A;
    }

    public static class TreeItem {
        public TreeItem left;
        public TreeItem right;
        public TreeItem sibling;
        public String value;

        public TreeItem(String value) {
            this.value = value;
        }

        @Override
        public String toString() {
            return value;
        }
    }

}

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

Can we do with reverse level order traversal in zig zag form?

- miandfhyu August 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