Bazaarvoice Interview Question for Senior Software Development Engineers


Team: Platform and Products
Country: United States
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
8
of 8 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 0 vote

Convert the tree to an array representation. Then print the array.

- kr.neerav January 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// start with a list that contains only the root node
    private static void printRecursive(List<TreeNode> nodes, int direction) {
   
    	if (nodes.isEmpty()) return;
    	
    	List<TreeNode> successors = new LinkedList<>();
    	
    	for (int i = 0; i < nodes.size(); i++) {
    		System.out.println(direction == 1 ? nodes.get(i).value :
    				nodes.get(nodes.size() - i - 1).value);
    		
    		TreeNode node = nodes.get(i);
    		if (node.left != null) successors.add(node.left);
    		if (node.right != null) successors.add(node.right);
    	}
    	
    	printRecursive(successors, direction * -1); // change direction = zig zag
    }

- Anonymous January 27, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// start with a list that contains only the root node
    private static void printRecursive(List<TreeNode> nodes, int direction) {
   
    	if (nodes.isEmpty()) return;
    	
    	List<TreeNode> successors = new LinkedList<>();
    	
    	for (int i = 0; i < nodes.size(); i++) {
    		System.out.println(direction == 1 ? nodes.get(i).value :
    				nodes.get(nodes.size() - i - 1).value);
    		
    		TreeNode node = nodes.get(i);
    		if (node.left != null) successors.add(node.left);
    		if (node.right != null) successors.add(node.right);
    	}
    	
    	printRecursive(successors, direction * -1); // change direction = zig zag
    }

- CodeCraft January 27, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// start with a list that contains only the root node
    private static void printRecursive(List<TreeNode> nodes, int direction) {
   
    	if (nodes.isEmpty()) return;
    	
    	List<TreeNode> successors = new LinkedList<>();
    	
    	for (int i = 0; i < nodes.size(); i++) {
    		System.out.println(direction == 1 ? nodes.get(i).value :
    				nodes.get(nodes.size() - i - 1).value);
    		
    		TreeNode node = nodes.get(i);
    		if (node.left != null) successors.add(node.left);
    		if (node.right != null) successors.add(node.right);
    	}
    	
    	printRecursive(successors, direction * -1); // change direction = zig zag
    }

- CodeCraft January 27, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 4 vote

Use to stacks and print them interchangeably.

public static void zigzag(BinaryTreeNode node) {
	
	Stack<BinaryTreeNode> currentStack = new Stack<BinaryTreeNode>();
	Stack<BinaryTreeNode> nextStack = new Stack<BinaryTreeNode>();
	currentStack.add(node);
	zigzag(currentStack, nextStack, true);
}

private static void zigzag(Stack<BinaryTreeNode> currentStack, Stack<BinaryTreeNode> nextStack, boolean reverse) {
	
	while (!currentStack.isEmpty()) {
		BinaryTreeNode pop = currentStack.pop();
		System.out.println(pop);
		if (reverse) {
			if (pop.left != null)
				nextStack.push(pop.left);
			if (pop.right != null)
				nextStack.push(pop.right);
		} else {
			if (pop.right != null)
				nextStack.push(pop.right);
			if (pop.left != null)
				nextStack.push(pop.left);
		}
		zigzag(nextStack, currentStack, !reverse);
	}
}

- thelineofcode January 16, 2014 | 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