Amazon Interview Question for SDE1s


Team: Kindle
Country: India
Interview Type: Written Test




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

private static void zigZagTraversal(BinaryTreeNode root) {
		int leftToRight = 1;
		BinaryTreeNode temp;
		Queue<BinaryTreeNode> currentLevel = new LinkedBlockingDeque<BinaryTreeNode>();
		Stack<BinaryTreeNode> nextLevel = new Stack<BinaryTreeNode>();

		System.out.println("Zig Zag Traversal");
		currentLevel.add(root);
		while (!currentLevel.isEmpty()) {
			temp = currentLevel.poll();
			if (temp != null) {
				System.out.print(" "+temp.getData());
				if (leftToRight == 0) {
					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()) {
				System.out.println();
				leftToRight = 1 - leftToRight;
				while (!nextLevel.isEmpty()) {
					currentLevel.add(nextLevel.pop());
				}
			}

		}
	}

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

Read tree level by level.
Maintain 2 ArrayLists of integers, one to store the current parent nodes and one to store current children nodes.
Also maintain a boolean leftToRight that will switch from true to false on every iteration.

public void printZigZag(Node root) {
	boolean leftToRight = true;
	ArrayList<Integer> parents = new ArrayList<Integer>();
	ArrayList<Integer> children = new ArrayList<Integer>();

	parents.add(root);
	print(parents, leftToRight); // Helper method to print a list in a given direction

	while (parents.length > 0) {
		if (leftToRight) { // Next row will be left to right
			for (int i = 0; i < parents.length; i++) {
				if (parents[i].left != null) {
					children.add(parents[i].left);
				}
				if (parents[i].right != null) {
					children.add(parents[i].right);
				}
			}
		} else { // Next row will be right to left
			for (int i = parents.length - 1; i >= 0; i--) {
				if (parents[i].right != null) {
					children.add(parents[i].right);
				}
				if (parents[i].left != null) {
					children.add(parents[i].left);
				}
			}
		}
		print(parents, leftToRight);
		parents = children;
		leftToRight = !leftToRight;
	}
}

- JW May 13, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void zigZagTraverse(Node n){
		LinkedList<Node> l = new LinkedList<>();
		l.add(n);
		zigZagTraverseRecurse(l,true);
	}
	public void zigZagTraverseRecurse(LinkedList<Node> l, boolean reverse){
		LinkedList<Node> newL = new LinkedList<>();
		while(l.size()>0){
			Node n = reverse?l.pollLast():l.pollFirst();
			System.out.println(n.dat);
			if (n.left != null){
				newL.addLast(n.left);
			}
			if (n.right != null){
				newL.addLast(n.right);
			}
		}
		System.out.println("");
		if (newL.size()>0) {
			zigZagTraverseRecurse(newL,!reverse);
		}
	}

- jim May 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

while(l.size()>0)

I'm not sure where the semi colon is coming from in my ">0" checks here. It doesn't show in editor...

- jyanchus May 14, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The solution is simple:

1. Print the root as it is.
2. From now on : for each Even level : You use Queue as an implementation DS and print.
: for each Odd level : You use Stack as an implementation DS and print.
3. End

- Pranay Deshpande February 11, 2016 | 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