Directi Interview Question for Software Engineer / Developers


Country: India
Interview Type: Written Test




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

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

private static void zigzag(Stack<Node> currentStack, Stack<Node> nextStack,
boolean reverse) {
while(!currentStack.isEmpty()){
Node 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);
}
}

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

good solution using two stacks

- Siva June 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

the line

zigzag(nextStack, currentStack, !reverse);

should be after the while loop.

- SantiagoYemmiganur June 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

BFS

- lucifer June 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

See my implementation at codepad.org/ueUOmYjE
Look up the function "void bst_BFS_spiral(Node *pNode, bst_visit visit) " that implements the spiral traversal of the BST.

- ashot madatyan June 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.io.IOException;
import java.util.ArrayList;
class TreeNode {
	public TreeNode levelnext,next,left, right;
	public int data;
	TreeNode(int data) {
		this.data = data;
	}
}

public class ZIGZAGTRAVERSAL {
	public static TreeNode getRandomBST() {
		TreeNode mainroot = new TreeNode(7);
		TreeNode root = mainroot;
		root.left = new TreeNode(5);
		root.right = new TreeNode(12);
		root.left.left = new TreeNode(3);
		root.left.left.left = new TreeNode(2);
		
		root.left.right = new TreeNode(6);
		root.left.left.right = new TreeNode(4);
		root.right.left = new TreeNode(10);
		root.right.right = new TreeNode(14);
		return mainroot;
	}
	
	public static void zigZag(TreeNode node) {
		int direction = 1;
		ArrayList<TreeNode> alist = new ArrayList<TreeNode>();
		alist.add(node);
		int count1 = 1;
		int count2 = 0;
		while (alist.size() > 0) {
			if (direction == 1) {
				for (int i = 0; i < count1; i++) {
					TreeNode n = alist.get(i);
					if (n.left != null) {
						count2++;
						alist.add(n.left);
					}
					if (n.right != null) {
						count2++;
						alist.add(n.right);
					}
					
				}
				for (int i = 0; i < count1; i++) {
					System.out.print(alist.get(0).data + " ");
					alist.remove(0);
				}
				count1 = 0;
			} else {
				int pos2 = alist.size() - 1;
				int pos1 = alist.size() - count2;
				for (int i = pos1; i <= pos2; i++) {
					TreeNode n = alist.get(i);
					if (n.left != null) {
						count1++;
						alist.add(n.left);
					}
					if (n.right != null) {
						count1++;
						alist.add(n.right);
					}
				}

				for (int i = pos2; i >= pos1; i--) {
					System.out.print(alist.get(i).data + " ");
					alist.remove(i);
				}
				count2 = 0;
			}
			direction = direction ^ 1;
		}
	}
	public static void main(String args[]) throws IOException {
		TreeNode bst = getRandomBST();
		zigZag(bst);
	}
}

- salvo4u June 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The above code does not use 2 stacks just a single ArrayList

- salvo4u June 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

even though this uses a single arraylist, it does not mean that this is space efficient.
If you compare two stack implementation and this implementation,
you can see at all stages , both implementations uses the same memory.

More over, since this implementation takes little more time than the other, as it removes elements from one index to another.

and the implementation of two stacks.. is easier to read and implement

- Siva June 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

U r rite but the remove thing might not be true
* The solution are identical apart from the fact that u have to explicitly maintain 2 stacks i dont do that and travesre the arraylist in opposite direction.
* Secondly i manage things using 1 object only in ur case an extra object has to be maintained

- salvo4u June 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Making a binary search tree and using BSF on this tree.
with each entry in the que we change the flag..
and corresponding to that flag we print the queue members.

- Abhijeet July 06, 2012 | 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