Amazon Interview Question for Software Engineer / Developers






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

In the following element we have a new field called parsed in the node data structure.. and it is set to false initially.

We can use a stack...

1. first push the element and set the value of parsed for this element to true, then the right child, and then the left child. If not children, nothing to be be pushed on the stack.
2. Now pop the last element on the stack, see if it is parsed.. If not then follow step 1. If yes, go to step 3.
3. Print the element. and pop it out.

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

you cannot assume you are allowed to add a field to an exisiting data structure!

- geniusxsy February 07, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If we cannot assume an extra field in the data structure...
then put the element/node to be pushed on to stack in step 1 into a hash..
and in step 2... when u pop u pop.. see if the node is on the hash!!

- OM February 07, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Scanner;
import java.util.Stack;

public class PostOrder {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		Node root = readTree(in);
		Stack<Action> stack = new Stack<Action>();
		stack.push(new VisitNode(stack, root));
		while (!stack.empty()) {
			stack.pop().act();
		}
	}

	static Node readTree(Scanner in) {
		int value = in.nextInt();
		if (value == 0) {
			return null;
		} else {
			Node node = new Node();
			node.value = value;
			node.left = readTree(in);
			node.right = readTree(in);
			return node;
		}
	}
}

class Node {
	int value;
	Node left, right;
}

interface Action {
	void act();
}

class VisitNode implements Action {
	Stack<Action> stack;
	Node node;

	VisitNode(Stack<Action> s, Node n) {
		stack = s;
		node = n;
	}

	public void act() {
		if (node != null) {
			stack.push(new PrintValue(node.value));
			stack.push(new VisitNode(stack, node.right));
			stack.push(new VisitNode(stack, node.left));
		}
	}
}

class PrintValue implements Action {
	int value;

	PrintValue(int v) {
		value = v;
	}

	public void act() {
		System.out.println(value);
	}
}

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

Same exact thing in Ruby:

def read_tree
  val = gets.to_i
  if val == 0
    nil
  else
    node = Node.new
    node.val = val
    node.left = read_tree
    node.right = read_tree
    node
  end
end

class Node
  attr_accessor :val, :left, :right
end

def act(s, n)
  unless n.nil?
    s.push(lambda {puts n.val})
    s.push(lambda {act(s, n.right)})
    s.push(lambda {act(s, n.left)})
  end
end

root = read_tree
stack = [lambda {act(stack, root)}]
stack.pop[] until stack.empty?

- Ryan February 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void postOrder(Node root) {
Stack stack1, stack2;

stack1.push(root);
while(!stack1.isEmpty()){
Node tmp = stack1.pop();
stack2.push(tmp);
if(tmp.left != null) {
stack1.push(tmp.left);
}
if(tmp.right != null) {
stack1.push(tmp.right);
}
}

while(!stack2.isEmpty()) {
System.out.println(stack2.pop().data);
}
}

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

Nice solution using two stacks.. but am not sure abt the space efficiency

- XeqtR February 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

space efficiency is bad

a good solution use only space O(h) where h is height of tree.

- geniusxsy February 10, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

very nice soln...dont know about space efficiency..

- Anonymous March 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

wtf the solution posted is crap. And best part is people are appreciating a wrong answer

- wtf February 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

the sol is absolutely right...
for those who did not get it, here is the reference link:
wwwleetcodecom/2010/10/binary-tree-post-order-traversal.html

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

include a dot(.) after www and leetcode...

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

en.wikipedia.org/wiki/Tree_traversal

- tetura February 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

How clever of you. The answer is not on that page.

- Ryan February 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Okay kids...

void postorder() {
  stack<BSTNode**> s;
  BSTNode** n = &_root;
  BSTNode** prev = &_root;
  while (true) {
    if (n != NULL && *n != NULL) {
      s.push(n);
      n = &((*n)->left);
    } else {
      if (!s.empty()) {
        n = s.top();
        if (*prev == (*n)->right) {
          printf("%d\n", (*n)->data);
          s.pop();
          prev = n;
          n = NULL;
        } else {
          prev = n = &((*n)->right);
        }
      } else {
        break;
      }
    }
  }
}

- Ryan February 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

One similar solution using single stack :

void postorder (tree_node * root) {
stack<tree_node *> s;
tree_node *p_n = root, *temp = NULL, *prv = NULL;

if(root == NULL)
return;

while(true){
while(p_n != NULL){
s.push(p_n);
p_n = p_n->left;
}
temp = s.top();
if(prv == temp){
cout << prv->data << ",";
s.pop();
if(s.empty())
break;
} else {
if(temp->right != prv){
p_n = temp->right;
prv = temp;
}else {
cout << temp->data << ",";
s.pop();
prv = s.top();
if(prv->right == temp){
p_n = NULL;
}
else
p_n = prv->right;
}
}
}
}

- ZooZoo March 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

push(root) // push root into stack
while(true) // stack not empty
{
node = pop()
if(node->right == null && node->left == null)
print node->data
else
{
push(node);
if(node->right != null)
push(node->right)
if(node->left != null)
push(node->left)
}
}

- djm March 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void postOrder(Node root) {
Stack stack1, stack2;

stack1.push(root);
while(!stack1.isEmpty()){
Node tmp = stack1.pop();
stack2.push(tmp);
if(tmp.left != null) {
stack1.push(tmp.left);
}
if(tmp.right != null) {
stack1.push(tmp.right);
}
}

while(!stack2.isEmpty()) {
System.out.println(stack2.pop().data);
}
}


The solution given by anonymous doesnt even works. And people are praising him without solving it with a example.

- alahomora February 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@alahomora...great job..

- alsas June 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@alahomora very good!!

- Jay June 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void postOrderTraversalWithoutRecursion(Node node){
		if(node == null){
			return;
		}		
		Stack<Node> stack = new Stack<Node>();
		Node prev = null;
		
		if(node != null)
		stack.push(node);
		
		while (!stack.isEmpty()){
			Node curr = stack.peek();
			if(prev == null || prev.getLeft() == curr || prev.getRight() == curr){
				
				if(curr.getLeft() != null){
					stack.push(curr.getLeft());
				}	else if (curr.getRight() != null)	{
					stack.push(curr.getRight());
				}
			}	else if( curr.getLeft() == prev){
				if(curr.getRight() != null){
					stack.push(curr.getRight());
				}
			}	else	{
				System.out.print(curr.getValue());
				stack.pop();
			}
			prev = curr;
		}
	}

- Sivashankar.R May 17, 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